I needed a utility to help list image files that I keep in various directories. I wrote a short Python 3 script that met my needs. I thought I’d share it.
#!/usr/bin/env python3 # image2htm.py # MIT/X11 License # See http://jiml.us/license2018.htm from os import listdir if __name__ == "__main__": f=open("images.htm","w") f.write("<html><head><title>Image List</title></head><body>\n") for fname in listdir("."): if fname.lower().endswith((".jpg",".jpeg",".gif",".png")): f.write("<h2>" + fname + "<h2>") f.write("<img src='" + fname + "' style='max-width: 1000;'>") f.write("<p>\n") f.write("</body></html>") f.close()
Usage is simply:
An HTML file named images.htm will be created in the current directory. When opened, this file will display the filenames with a rendered picture of each image.
The images are scaled to a max of 1000 pixels in width.
I didn’t provide a configuration file option for the HTML filename or for any of the generated markup. The script itself is short enough that it should be pretty easy to change.
Things I learned: I had no idea until working on this script that the endswith() method can use a tuple of comparator strings.