I had a need for a streamlined thumbnail generator for a project.
I whipped this up the past couple days using python and the pillow library.
This is alpha software, but works okay.
It is very simple.
Input your max pixel size, select directory then process the files.
It will create a folder of resized images in a thumbnail subdirectory keeping the aspect ratio and using the pixel number as the largest edge.
I know you can do this with gimp scripts or photoshop, but this is lightweight, the logic can be pulled for other projects and I learned a bit about tkinter and guis.
I have tested this in linux and windows, don’t have a mac, so YMMV.
for the microsoft haters:
Just a quick tip: Instead of
if os.path.exists( str(pixels) + "_thumbs"):
os.mkdir(( str(pixels) + "_thumbs"))
you can use
str(pixels) + "_thumbs",
good tip, there are a few ways to handle this, I try and make my if / else statements more java looking in case I need to rewrite later
Does this speed things up?
In theory it’s gonna be faster because the check is done by fast OS code rather than python. In practice… don’t bother. I don’t think you’ll notice any difference unless you’re batch creating thousands of directories.
In general python follows the “it’s easier to ask for forgiveness than permission” approach: Checking ahead of time should be avoided. If something goes wrong just catch the exception and deal with it later.
that’s a great tip!
thanks for letting me know.
I was curious why so many times I see people push everything and then check for exceptions after, and now what you say makes a bit more sense to me.
I plan on building this into a more comprehensive system so I’ll certainly take your tip into consideration.
How about the checking for file types? Is that decent, or do you have a better way of doing it as well?
The section I’m thinking of is here:
Take a look at pathlib. It’s an object oriented approach to handling paths.
I’d do something like this (not tested, probably some minor errors in there):
from pathlib import Path
file_path = Path(file)
if file.suffix in ('.jpg', '.jpeg', '.png'):