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

后端 未结 20 1861
-上瘾入骨i
-上瘾入骨i 2020-11-22 02:30

Is there an obvious way to do this that I\'m missing? I\'m just trying to make thumbnails.

20条回答
  •  醉话见心
    2020-11-22 03:00

    Based in @tomvon, I finished using the following (pick your case):

    a) Resizing height (I know the new width, so I need the new height)

    new_width  = 680
    new_height = new_width * height / width 
    

    b) Resizing width (I know the new height, so I need the new width)

    new_height = 680
    new_width  = new_height * width / height
    

    Then just:

    img = img.resize((new_width, new_height), Image.ANTIALIAS)
    

提交回复
热议问题