Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL: Thumbnail and end up with a square image

Calling

image = Image.open(data) image.thumbnail((36,36), Image.NEAREST) 

will maintain the aspect ratio. But I need to end up displaying the image like this:

<img src="/media/image.png" style="height:36px; width:36px" /> 

Can I have a letterbox style with either transparent or white around the image?

like image 929
Paul Tarjan Avatar asked Sep 06 '09 18:09

Paul Tarjan


People also ask

What is PIL image image?

PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image.

How do I resize an image using PIL and maintain its aspect ratio?

To resize an image using PIL and maintain its aspect ratio with Python, we can open the image with Image. open . Then we calculate the new width and height to scale the image to according to the new width. And then we resize the image with the resize method and save the new image with the save method.


1 Answers

PIL already has a function to do exactly that:

from PIL import Image, ImageOps thumb = ImageOps.fit(image, size, Image.ANTIALIAS) 
like image 61
Cesar Canassa Avatar answered Oct 10 '22 00:10

Cesar Canassa