PIL: Thumbnail and end up with a square image

前端 未结 7 776
一个人的身影
一个人的身影 2020-12-22 22:33

Calling

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

will maintain the aspect ratio. But I need to end up displaying th

7条回答
  •  长情又很酷
    2020-12-22 23:06

    you can wrap Nadia's answer in this function, which gives you control over size and background.

    def make_square(im, min_size=36, fill_color=(255, 255, 255, 0)):
        x, y = im.size
        size = min(min_size, x, y)
        new_im = Image.new('RGBA', (size, size), fill_color)
        im.thumbnail((256, 256))
        new_im.paste(im, (int((x - size) / 2), int((y -size) / 2))
        return new_im
    

提交回复
热议问题