A Python Script for Photo Resizing (a.k.a. Staying Under Google Photo's Size Limit)
My photo workflow usually involves me working on a folder full of raw files which I then process using RawStudio. It lets me quickly process a bunch of raw files and I end up spitting out full size jpeg files of the keepers into a subfolder. This all works well. Except for the fact that…
I use Google Photos. It provides great sharing ability for photos. And it offers free unlimited photo storage if the photos are less than 16MP in size. The photos from my Canon 7D are 18MP. So inevitably I need to resize those photos so they’re under the 16MP limit.
Typically I use ImageMagick and try to scale down the photos using some guesstimated scale factor. There are two problems. One.. The guesstimation. And two, not all the photos are necessarily over the 16MP limit. I may have done some cropping adjustments in RawStudio which may have already left some of them under the limit. So it’s a messy, inefficient and yucky solution to the problem.
I was thinking about writing a bash script for this, but good buddy Tim Wesline being smarter than I, gently pointed me towards using Python instead. So I’ve just written a super simple python script that solves this for me. I’ve made the script executable and put it in my system’s path, so it can be run from any folder full of photos. As has been my typical experience with Python, it ended up being much simpler than I anticipated. Here’s what it does:
- Checks the size of each jpg file and only processes ones that are over 16MP.
- Resizes the ones that are bigger than 16MP down to a size slightly under the limit (15.9MP or thereabouts).
- Doesn’t overwrite any files, but instead creates a resized copy that has a _rszd suffix on the filename.
So I can now run this on a folder full of jpeg files and quickly see which ones have had to be resized, and can quickly pick out the ones I upload to Google Photos.
I’m a lazy bastard, so the 15.9MP limit is hardcoded in (as is the 16MP limit) but obviously this could be easily repurposed for other limits etc.
I’m also seeing that the resized photos are quite a bit smaller in terms of filesize, which is only natural, but it also reminded me that I should check into the resize method of PIL to see if it maintains image quality. Looking at full res versions of the files, it looks like it does.
Here is the script. If you want to mess with it and improve it, by all means go ahead and let me know.
#!/usr/bin/python """ This is a simple script that resizes any JPG files that are over 16MP in size down to 15900000 so that they will fit within Google Photo's limitation for unlimited free storage. It will not overwrite any files, but will only create resized copies (with an underscored suffix) of the ones that were over the limit. Authored by Richard Querin <[email protected]> January 2016 """ import os import math import glob from PIL import Image # this script should take all jpg files from current directory and resize them so they are less than 16MP while maintaining aspect ratio etc # get list of jpgs in the current directory filenames = glob.glob('./*.jpg') # process each jpg file for filename in filenames: #get image size and assign to width and height variables image = Image.open(filename) width = float(image.size) height = float(image.size) imagesize = width * height aratio = width/height #if imagesize is over 16 MP, then process if imagesize > 16000000: newheight = int(round(math.sqrt(15900000/aratio))) newwidth = int(round(aratio * newheight)) newfilename = filename[:-4] + "_rszd" + filename[-4:] image.resize((newwidth,newheight)).save(newfilename)