Introducing LoBIP - a Python Thumbnail Generator

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:
https://gitlab.com/jdfthetech/lobip

2 Likes

Just a quick tip: Instead of

def makeDir(pixels):
    if os.path.exists( str(pixels) + "_thumbs"):
        pass
    else:
       os.mkdir(( str(pixels) + "_thumbs"))

you can use

os.makedirs(
    str(pixels) + "_thumbs",
    exist_ok=True
)
1 Like

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.

1 Like

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:

    if file.endswith('.jpg'):
        imageProcess()

    elif file.endswith('.jpeg'):
        imageProcess()

    elif file.endswith('.png'):
        imageProcess()

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'):
    imageProcess()
1 Like