Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a folder of images and print images inline in Jupyter notebook with Python 3.6

My following code iterates through a list of files & prints each image "inline" in a Jupyter Notebook with the filename.

from IPython.display import Image, display
listOfImageNames = ["0/IMG_1118.JPG","0/IMG_1179.JPG"]
for imageName in listOfImageNames:
    display(Image(filename=imageName))
    print(imageName)

However, I want to achieve the same output, but iterate through a folder of images, without having to reference each image file in the code. I have been battling with this one- can anyone point me in the right direction?

like image 755
Steve Avatar asked Sep 17 '25 16:09

Steve


2 Answers

Using glob to search for JPG files!

import glob
from IPython.display import Image, display
for imageName in glob.glob('yourpath/*.jpg'): #assuming JPG
    display(Image(filename=imageName))
    print(imageName)
like image 161
ibarrond Avatar answered Sep 20 '25 04:09

ibarrond


If your images are present in different folders and on different levels, you should approach recursively:

from IPython.display import Image, display
from glob import glob
listofImageNames = glob('**/*.JPG', recursive=True)
for imageName in listOfImageNames:
    display(Image(filename=imageName))
    print(imageName)
like image 44
Krishna Avatar answered Sep 20 '25 04:09

Krishna