Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Building a loop in python


In my file pool I have a bunch of images, all called <name>.jpg. I want to use each image with its corresponding name in a loop (basically, this is for the familiarisation phase of an experiment).

Given a list called filename and one called name, made like this:

# get all images in the file pool (all .jpg format)
filename = [image for image in pool if ".jpg" in image]

# images are named after their content, so these are the image names
name = [image.rsplit(".",1)[0] for image in filename

is there a way I can populate a loop datamatrix so that it looks as below, and then use it?

name filename
apple apple.jpg
banana banana.jpg
... ...

Thanks in advance!


  • Hello,

    Yes you can do it with DataMatrix:

    The strategy I use (but there might be something better) is to get an inline at the very beginning of my script where I do stuffs to list (randomizing, name... whatever) and populate.

    from datamatrix import DataMatrix
    # do stuffs to lists
    filename = range(10)
    filename = [ str(x)+ '.png' for x in filename]
    name = range(10)
    # Populate
    dm_loop = DataMatrix(length=len(filename))
    # fill the first column with the randomized list
    dm_loop.filename = filename
    # initialize and fill the two other columns = name
    items['new_loop'].dm = dm_loop

    Then having a loop that is empty and sequential. Insert in this loop the sequence (classical).

    Please find the script attached. Not sure if it is the best strategy but that is the one I use. I hope it helps.



    Thanked by 1martinc
  • Thanks Sylvain, that works perfectly :smile:

Sign In or Register to comment.