import Image
image = Image.open('images/original.jpg')
width = image.size[0]
height = image.size[1]
if width > height:
difference = width - height
offset = difference / 2
resize = (offset, 0, width - offset, height)
else:
difference = height - width
offset = difference / 2
resize = (0, offset, width, height - offset)
thumb = image.crop(resize).resize((200, 200), Image.ANTIALIAS)
thumb.save('thumb.jpg')
This is my current thumbnail generation script. The way it works is:
If you have an image that is 400×300 and you want a thumbnail that’s 100×100, it will take 50 pixels off the left and right side of the original image. Thus, resizing it to be 300×300. This gives the original image the same aspect ratio as the new thumbnail. After that, it will shrink it down to the required thumbnail size.
The advantages of this is:
- The thumbnail is taken from the center of the image
- Aspect ratio doesn’t get screwed up
If you were to shrink the 400×300 image down to 100×100, it will look squished. If you took the thumbnail from 0x0 coordinates, you would get the top left of the image. Usually, the focal point of the image is the center.
What I want to be able to do is give the script a width/height of any aspect ratio. For example, if I wanted the 400×300 image to be resized to 400×100, it should shave 150px off the left and right sides of the image…
I can’t think of a way to do this. Any ideas?
You just need to compare the aspect ratios – depending on which is larger, that will tell you whether to chop off the sides or the top and bottom. e.g. how about: