-1

I have several .pngs that are 586 x 428. I need to convert them into .pngs that are 512 x 512. I want to do this without stretching the image at all. So basically, I want to scale the width down to 512, then add an equal amount of transparency in the height both above and below the original image.

So, the final product would be a .png that is 512 x 512, and 69px of transparency in height on top and bottom, leaving the "colored" part of the image to be 512 x 374.

I am trying to do this with Python Imaging Library. I am pretty new to this, so if someone can help me with this, that would be much appreciated.

hitch45
  • 472
  • 4
  • 12
  • 1
    Have you done any research on this before posting? [This question](http://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio) combined with [this other one](http://stackoverflow.com/questions/4379978/python-pil-how-to-make-area-transparent-in-png) should solve your question. – inspectorG4dget Sep 06 '12 at 20:34
  • Word of advise, in addition to the previous comment... these kinds of questions will almost always require a code example of what you have so far. Otherwise the only answer someone can give is just a complete solution. We don't know where to start from what you have. – jdi Sep 06 '12 at 21:08
  • The exact code from the [first example in my answer to this duplicate question](http://stackoverflow.com/a/9103783/496445) should work for your problem. – jdi Sep 06 '12 at 23:45
  • Look at @jdi's answer (linked in the above comment) and the second answer linked in my comment.Those two answers will get you where you need to go – inspectorG4dget Sep 07 '12 at 23:21
  • possible duplicate of [Resize image maintaining aspect ratio AND making portrait and landscape images exact same size?](http://stackoverflow.com/questions/9103257/resize-image-maintaining-aspect-ratio-and-making-portrait-and-landscape-images-e) – User97693321 Sep 08 '12 at 03:55

2 Answers2

3
from PIL import Image

def adjust(image):
    image = image.convert('RGBA')
    width, height = image.size
    new_width = 512
    new_height = new_width * height // width
    image = image.resize((new_width, new_height), resample=Image.ANTIALIAS)
    new_image = Image.new('RGBA', (512, 512), (0, 0, 0, 0))
    upper = (512 - image.size[1]) // 2
    new_image.paste(image, (0, upper))
    return new_image
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

Alright, it's been two hours since you posted the question. Since you haven't posted code and nobody has posted an answer, here's something that'll get you started:

  1. Load the source image
  2. Resize the source image to 512x534
  3. Make a new image of size 512x512
  4. Copy over the resized pixels into a rectangle in the new image whose corners are (0, 69, 512, 441)
  5. Make the following two rectangles transparent (0,0,512,69) and (0,441,512,512)

Hope that helps you get started

Community
  • 1
  • 1
inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • i wrote a module that does most of this https://github.com/jvanasco/imagehelper one could hack out the relevant bits from that and use the clues above to have something fully functional in little time. – Jonathan Vanasco Sep 07 '12 at 01:03