PIL: Thumbnail and end up with a square image

前端 未结 7 763
一个人的身影
一个人的身影 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:19

    from PIL import Image
    
    import StringIO
    
    def thumbnail_image():
        image = Image.open("image.png")
        image.thumbnail((300, 200))
        thumb_buffer = StringIO.StringIO()
        image.save(thumb_buffer, format=image.format)
        fp = open("thumbnail.png", "w")
        fp.write(thumb_buffer.getvalue())
        fp.close()
    

提交回复
热议问题