0

I would like to use Python (and the PIL library) to resize a picture (eventually with width > height or height > width) in a fixed size, let's say (144px * 144px).

I've already found topics about cropping the picture, but I would like to :

  • Find the biggest side of the picture
  • Do a thumbnail of the picture with biggest side = 144px and the other side = a ratio
  • Center the smallest side
  • Fill the borders of the smallest side with a transparent background so that the final picture is (144px * 144px)

Any ideas on how to do that? For the two first points I would use :

image = PIL.Image.open(pic)
image.thumbnail((144,144), PIL.Image.BILINEAR)

But then I'm blocked. Thanks!

Jerome
  • 1,225
  • 2
  • 12
  • 23
  • There is a method i read about a while ago which uses entropy to centre the crop on the most content rich part of the image. This might give you even better results. Let me find it. – will Jun 24 '14 at 11:04
  • possible duplicate of [How do I resize an image using PIL and maintain its aspect ratio?](http://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio) – will Jun 24 '14 at 11:10
  • I think you missed some parts of my question. This other thread talks about resizing an image and maintaining aspect ratio. I also want to center the picture on the smallest side. The function "thumbnail" doesn't do it, it simply resizes and keeps the ratio... Thx – Jerome Jun 24 '14 at 11:55
  • Well a thumbnail shouldn't crop the image i don't think, only shrink it down (hence giving it the `BILINEAR` argument). If i were you, i would think about the maths you're doing to get the coords of the image you want - it is fairly trivial - and then just look at the crop and resize functions of PIL. It should be pretty self explanatory. – will Jun 24 '14 at 18:02

0 Answers0