Why does PIL thumbnail not resizing correctly?

前端 未结 2 1069
广开言路
广开言路 2021-02-09 16:14

I am trying to create and save a thumbnail image when saving the original user image in the userProfile model in my project, below is my code:

def s         


        
2条回答
  •  隐瞒了意图╮
    2021-02-09 16:32

    Given:

    import Image # Python Imaging Library
    THUMB_SIZE= 45, 45
    image # your input image
    

    If you want to resize any image to size 45×45, you should use:

    new_image= image.resize(THUMB_SIZE, Image.ANTIALIAS)
    

    However, if you want a resulting image of size 45×45 while the input image is resized keeping its aspect ratio and filling the missing pixels with black:

    new_image= Image.new(image.mode, THUMB_SIZE)
    image.thumbnail(THUMB_SIZE, Image.ANTIALIAS) # in-place
    x_offset= (new_image.size[0] - image.size[0]) // 2
    y_offset= (new_image.size[1] - image.size[1]) // 2
    new_image.paste(image, (x_offset, y_offset))
    

提交回复
热议问题