Python: Image resizing: keep proportion - add white background

前端 未结 3 972
误落风尘
误落风尘 2021-01-02 16:50

I would like to create a Python script to resize images, but not changing its proportions, just by adding a white background

(So, a : 500*700 px im

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-02 17:31

    Thanks @Jay D., here a bit more general version:

    from PIL import Image
    
    def resize(image_pil, width, height):
        '''
        Resize PIL image keeping ratio and using white background.
        '''
        ratio_w = width / image_pil.width
        ratio_h = height / image_pil.height
        if ratio_w < ratio_h:
            # It must be fixed by width
            resize_width = width
            resize_height = round(ratio_w * image_pil.height)
        else:
            # Fixed by height
            resize_width = round(ratio_h * image_pil.width)
            resize_height = height
        image_resize = image_pil.resize((resize_width, resize_height), Image.ANTIALIAS)
        background = Image.new('RGBA', (width, height), (255, 255, 255, 255))
        offset = (round((width - resize_width) / 2), round((height - resize_height) / 2))
        background.paste(image_resize, offset)
        return background.convert('RGB')
    

提交回复
热议问题