'How to run iterations in Python with mini-batches of 4 pictures from a folder containing 40 pictures in total?

I want to run my image processing algorithm on a batch of 4 images and display the output. Then again, I want to run another iteration with another batch of 4 images, until all the images in my image-containing-folder are processed. Right now, the algorithm processes all pictures in the folder as a single batch.

I will put some codes showing how I am taking images from the folder and how I am creating the list. PS I'm super beginner at this.

def arg_parse():
parser.add_argument("--images",
                            dest='images',
                            help="Image / Directory containing images to  vehicle detection upon",
                            default="vehicles-on-lanes",
                            type=str)
......
images = args.images
imlist = [
            osp.join(osp.realpath('.'), images, img) for img in os.listdir(images)
        ]


Solution 1:[1]

Simply split list into smaller sublists imlist[0:4], imlist[4:8], ... ,imlist[n:n+4] and run your function(s) with every sublist separatelly

In this situation can be useful for-loop with range(len())

for n in range(0, len(imlist), 4): 
    your_function( imlist[n:n+4] )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furas