Does Python PIL resize maintain the aspect ratio?

前端 未结 4 1485
南笙
南笙 2021-01-01 17:13

Does PIL resize to the exact dimensions I give it no matter what? Or will it try to keep the aspect ratio if I give it something like the Image.ANTIALIAS argume

相关标签:
4条回答
  • 2021-01-01 17:25

    Yes. the thumbnail() method is what is needed here... One thing that hasn't been mentioned in this or other posts on the topic is that 'size' must be either a list or tuple. So, to resize to a maximum dimension of 500 pixels, you would call: image.thumbnail((500,500), Image.ANTIALIAS)

    See also this post on the subject: How do I resize an image using PIL and maintain its aspect ratio?

    0 讨论(0)
  • 2021-01-01 17:30

    Yes it will keep aspect ratio using thumbnail method:

    image = Image.open(source_path)
    image.thumbnail(size, Image.ANTIALIAS)
    image.save(dest_path, "JPEG")
    
    0 讨论(0)
  • 2021-01-01 17:33

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

    Image.resize from PIL will do exactly as told. No behind scenes aspect ratio stuff.

    0 讨论(0)
  • 2021-01-01 17:33

    No, it does not. But you can do something like the following:

    im = PIL.Image.open("email.jpg"))
    width, height = im.size
    im = im.resize((width//2, height//2))
    

    Here the height and width are divided by the same number, keeping the same aspect ratio.

    0 讨论(0)
提交回复
热议问题