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?
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With