【问题标题】:Python: Image resizing: keep proportion - add white backgroundPython:图像调整大小:保持比例 - 添加白色背景
【发布时间】:2017-11-06 07:36:40
【问题描述】:

我想创建一个 Python 脚本来调整图像大小,但不改变其比例,只需添加一个白色背景

(因此,一个 500*700 像素的图像将通过在每侧添加 100 像素的白色带转换为 700*700 像素的图像)

我使用的三种图像类型是 .PNG、.JPG 和 .GIF。我什至不确定 Gif、PNG 和 JPG 是否可能已经很棒了。

在我的情况下,它们必须是正方形。但是如果你们中的任何一个人设法做到适应任何比例,这将有利于看到这个线程的最大数量的人,你会更棒!

我看到了其他语言的相同线程,但没有看到 python,你们知道你是怎么做到的吗?

PS:我使用的是 Python 3

我尝试了什么:

将 3 张图像组合在一起。

如果我们拍摄 500*700 像素的图片: 创建两个 100*700px 的白色图像,并在图像的每一侧放置一个。灵感来源:

Combine several images horizontally with Python

但是,我对python有点陌生,我还没有成功。

【问题讨论】:

  • 如果您将图像表示为 2D numpy 矩阵,您可以调用一个 numpy 例程来填充它们。
  • SO 不是代码编写服务。请发布您到目前为止所获得的代码,并明确告诉我们您卡在哪一部分。最好提供Minimal, Complete, and Verifiable example
  • 我宁愿建议创建一张所需大小的全白图像,然后将输入图像居中粘贴到它上面。毫无疑问,PIL 模块可以完成这项工作。

标签: python image image-processing image-resizing


【解决方案1】:

终于做到了:

def Reformat_Image(ImageFilePath):

    from PIL import Image
    image = Image.open(ImageFilePath, 'r')
    image_size = image.size
    width = image_size[0]
    height = image_size[1]

    if(width != height):
        bigside = width if width > height else height

        background = Image.new('RGBA', (bigside, bigside), (255, 255, 255, 255))
        offset = (int(round(((bigside - width) / 2), 0)), int(round(((bigside - height) / 2),0)))

        background.paste(image, offset)
        background.save('out.png')
        print("Image has been resized !")

    else:
        print("Image is already a square, it has not been resized !")

感谢@Blotosmetek 的建议,粘贴居中的图像绝对比创建图像并组合它们更简单!

PS:如果你还没有 PIL,用 pip 安装它的库名称是“pillow”,而不是 PIL。但是,您仍然在代码中将其用作 PIL。

【讨论】:

  • 嗨,刚看到这个问题,有人能告诉我如何使用它来创建 9:16 的图像吗?
【解决方案2】:

感谢@Jay D.,这里有一个更通用的版本:

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')

【讨论】:

  • 确实!比例调整很棒!
  • 上面的功能更新了,当图片不是正方形的时候我遇到了一个错误。
【解决方案3】:

另一个答案对我不起作用,我重写了它,这有效:

def resize_with_pad(im, target_width, target_height):
    '''
    Resize PIL image keeping ratio and using white background.
    '''
    target_ratio = target_height / target_width
    im_ratio = im.height / im.width
    if target_ratio > im_ratio:
        # It must be fixed by width
        resize_width = target_width
        resize_height = round(resize_width * im_ratio)
    else:
        # Fixed by height
        resize_height = target_height
        resize_width = round(resize_height / im_ratio)

    image_resize = im.resize((resize_width, resize_height), Image.ANTIALIAS)
    background = Image.new('RGBA', (target_width, target_height), (255, 255, 255, 255))
    offset = (round((target_width - resize_width) / 2), round((target_height - resize_height) / 2))
    background.paste(image_resize, offset)
    return background.convert('RGB')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多