Image resize python script.

A python script for resizing images.

Posted by Matthew Regis on Mon, Nov 14, 2016
In Development, Python
Tags scripting, code, python, development

Intro

At one point I needed to resizes alot of images down in size. I looked around and played with a few programs but none did what I wanted it to do so I ended up creating this quick simple script.

What it does is take a bunch of images from the same directory and tries to resize them while trying to keep the aspect ratio. In this example I set the width to 600 and the height will be changed relative to the width it was before. Hope that makes sense. For Example if a image was 1000h x 800w, after you run the script you’ll find it’s 800h x 600w.

What you have to do is place all your images in the same folder as the python script. My naming convention was “pic_{number here}.jpg”. So to run this script as is you would have to name all your images to this.

Code

from PIL import Image

def main():
	count = 1
	while count < 3:
		resizeImage(count)
		count = count + 1


def resizeImage(picNumber):
	img = Image.open("pic_" + str(picNumber) + ".jpg")
	width, height = img.size
	if width > height and height > 600:
		width = width - ((width / height) * (height - 600))
		height = height - (height - 600)
	elif height > width and width > 600:
		height = height - ((height / width) * (width - 600))
		width = width - (width - 600)
	elif height == width and height > 600 and width > 600:
		height = 600
		width = 600
	img = img.resize((int(width), int(height)))
	img.save("pic_" + str(picNumber) + ".jpg", format='JPEG')



if __name__ == "__main__":
    main()

Break Down

There is only one import but you might need to install it. You’ll have to look at installing Pillow relative to your platform. I ran this on a mac.

pip install Pillow

What is Pillow? It’s this https://python-pillow.org/. It’s a image libary for python.

from PIL import Image

I update the main method to how many images I want to resize. For example if I have 500 images then I update to 501.

while count < 501

Below you can see it’s set to 3 so I only have 2 images I want to resize.

def main():
	count = 1
	while count < 3:
		resizeImage(count)
		count = count + 1

The resize image function. You can see I use the Image libary, I get the image then get it’s size then work out what the height should be if I resize the width to 600.

Once I have worked out the height and width I use the Image libary again to resize the image and then save it to disk, simples.

def resizeImage(picNumber):
	img = Image.open("pic_" + str(picNumber) + ".jpg")
	width, height = img.size
	if width > height and height > 600:
		width = width - ((width / height) * (height - 600))
		height = height - (height - 600)
	elif height > width and width > 600:
		height = height - ((height / width) * (width - 600))
		width = width - (width - 600)
	elif height == width and height > 600 and width > 600:
		height = 600
		width = 600
	img = img.resize((int(width), int(height)))
	img.save("pic_" + str(picNumber) + ".jpg", format='JPEG')

This is just the start of the script that calls main().

if __name__ == "__main__":
    main()

Summary

The script served it’s purpose, what would of taken me hours manually only took me a few minutes after coding it. Here I’m just resizing an image, with the Image libary you can do so much more. http://effbot.org/imagingbook/image.htm have a look for yourself of what you can do.


comments powered by Disqus