Python:图像调整大小:保持比例 - 添加白色背景
Python: Image resizing: keep proportion - add white background
我想创建一个 Python 脚本来调整图像大小,但不改变其比例,只需添加白色背景
(因此,通过在每侧添加 100 像素的白色条带,500*700 像素的图像将转换为 700*700 像素的图像)
我使用的三种图像类型是.PNG、.JPG 和.GIF。我什至不确定 Gif、PNG 和 JPG 是否已经很棒了。
在我的例子中,它们必须是正方形。但是如果你们中的任何一个能够做到适应任何比例,这将有利于最大数量的人看到这个线程,你会更棒!
我看到了其他语言的相同话题,但没有 python,你们知道你们是怎么做到的吗?
PS : 我正在使用 Python 3
我试过的:
将 3 张图像组合在一起。
如果我们拍摄 500*700 像素的图片:
创建两个 100*700px 的白色图像,并在图像的每一侧放置一个。灵感来自:
但是,我是 python 的新手,我还没有成功。
终于做到了:
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。
谢谢@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')
另一个答案对我不起作用,我重写了它并且这个有效:
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')
接受的答案很棒,我很高兴不用OpenCV。
如@Nemanja 所述,如果你想让它适用于任何纵横比。这是要使用的代码段。我只是稍微扭曲了代码。
from PIL import Image
def Reformat_Image_With_Ratio(ImageFilePath, desired_aspect_ratio):
image = Image.open(ImageFilePath, 'r')
width = image.width
height = image.height
img_aspect_ratio = width/height
if (img_aspect_ratio != desired_aspect_ratio):
bigside = width if width > height else height
other_side = int(bigside * desired_aspect_ratio)
background = Image.new('RGBA', (other_side, bigside), (255, 0, 0, 255))
offset = (int(round(((bigside - width) / 2), 0)), int(round(((bigside - height) / 2),0)))
background.paste(image, offset)
background.save('out4.png')
print("Image has been resized !")
else:
print("Image is already a valid aspect ratio, it has not been resized !")
Reformat_Image_With_Ratio('test.png', 9/16)
我想创建一个 Python 脚本来调整图像大小,但不改变其比例,只需添加白色背景
(因此,通过在每侧添加 100 像素的白色条带,500*700 像素的图像将转换为 700*700 像素的图像)
我使用的三种图像类型是.PNG、.JPG 和.GIF。我什至不确定 Gif、PNG 和 JPG 是否已经很棒了。
在我的例子中,它们必须是正方形。但是如果你们中的任何一个能够做到适应任何比例,这将有利于最大数量的人看到这个线程,你会更棒!
我看到了其他语言的相同话题,但没有 python,你们知道你们是怎么做到的吗?
PS : 我正在使用 Python 3
我试过的:
将 3 张图像组合在一起。
如果我们拍摄 500*700 像素的图片: 创建两个 100*700px 的白色图像,并在图像的每一侧放置一个。灵感来自:
但是,我是 python 的新手,我还没有成功。
终于做到了:
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。
谢谢@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')
另一个答案对我不起作用,我重写了它并且这个有效:
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')
接受的答案很棒,我很高兴不用OpenCV。
如@Nemanja 所述,如果你想让它适用于任何纵横比。这是要使用的代码段。我只是稍微扭曲了代码。
from PIL import Image
def Reformat_Image_With_Ratio(ImageFilePath, desired_aspect_ratio):
image = Image.open(ImageFilePath, 'r')
width = image.width
height = image.height
img_aspect_ratio = width/height
if (img_aspect_ratio != desired_aspect_ratio):
bigside = width if width > height else height
other_side = int(bigside * desired_aspect_ratio)
background = Image.new('RGBA', (other_side, bigside), (255, 0, 0, 255))
offset = (int(round(((bigside - width) / 2), 0)), int(round(((bigside - height) / 2),0)))
background.paste(image, offset)
background.save('out4.png')
print("Image has been resized !")
else:
print("Image is already a valid aspect ratio, it has not been resized !")
Reformat_Image_With_Ratio('test.png', 9/16)