Grab Nth image from folder

For inference, I’d like to grab the nth image from a folder (say the first image), but when I do this:

images = path/"images"
nth_image = images[0]
#img = PILImage.create(nth_image)

I get the following error:

TypeError: 'PosixPath' object does not support indexing

Is there a quick and easy way to effectively ‘index’ into this path (without literally indexing as it appears to be unsupported)?


The solution (for anybody else searching for the same thing) is to use the get_image_files function like so:

images = path/"images"
items = get_image_files(images)
img = items[0]
img = PILImage.create(img)

your images variable is a PosixPath representing your image directory. To get the (image) files inside the directory, you can use / (i.e. ls stands for list), where n is the number of files you wanna grab, leaving it empty will grab all files in the directory.

So you can do something like this to grab the first file in the directory:

images = path/'images'
nth_image =[0]

As a remark, nth_image is also a PosixPath, representing the path to the image file

Oh nice! I did not know that! Thanks for the tip :+1: