我想将矩形图像转换为方形图像。 但是,我有问题。
我的试用Python代码在这里:
from PIL import Image
import numpy as np
im = Image.open('aa.png')
pixMap = im.load()
img = Image.new( im.mode, im.size)
sqrWidth = np.ceil(np.sqrt(im.size[0]*im.size[1])
pixNew = Image.new(im.mode, (im.size[0]*im.size[1], 1))
pixSave = Image.new(im.mode, (sqrWidth, sqrWidth))
k=0
for i in range(img.size[0]):
for j in range(img.size[1]):
pixNew[k] = pixMap[i, j]
k=k+1
k=0
for i in range(sqrWidth):
for j in range(sqrWidth):
pixSave[i, j] = pixNew[k]
k=k+1
im.close()
img.show()
img.save("out.png")
img.close()
我的错误在这里:
Traceback (most recent call last):
File "rect2square.py", line 13, in <module>
pixNew[k] = pixMap[i, j]
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in __getattr__
raise AttributeError(name)
AttributeError: __setitem__
怎么了?
答案 0 :(得分:2)
您可以在PIL中使用调整大小图像过滤器。
from PIL import Image
import numpy as np
im = Image.open('image.png')
sqrWidth = np.ceil(np.sqrt(im.size[0]*im.size[1])).astype(int)
im_resize = im.resize((sqrWidth, sqrWidth))
im_resize.save('output.png')
这会将您的图像压缩成正方形。这是你想要的吗?
答案 1 :(得分:2)
我在做 API 时遇到了这个问题。其他解决方案都没有真正回答 OP,因为他们建议减少比率或添加背景。以下解决方案将图像的中心部分保持为正方形。
def resize_image(self, image: Image, length: int) -> Image:
"""
Resize an image to a square. Can make an image bigger to make it fit or smaller if it doesn't fit. It also crops
part of the image.
:param self:
:param image: Image to resize.
:param length: Width and height of the output image.
:return: Return the resized image.
"""
"""
Resizing strategy :
1) We resize the smallest side to the desired dimension (e.g. 1080)
2) We crop the other side so as to make it fit with the same length as the smallest side (e.g. 1080)
"""
if image.size[0] < image.size[1]:
# The image is in portrait mode. Height is bigger than width.
# This makes the width fit the LENGTH in pixels while conserving the ration.
resized_image = image.resize((length, int(image.size[1] * (length / image.size[0]))))
# Amount of pixel to lose in total on the height of the image.
required_loss = (resized_image.size[1] - length)
# Crop the height of the image so as to keep the center part.
resized_image = resized_image.crop(
box=(0, required_loss / 2, length, resized_image.size[1] - required_loss / 2))
# We now have a length*length pixels image.
return resized_image
else:
# This image is in landscape mode or already squared. The width is bigger than the heihgt.
# This makes the height fit the LENGTH in pixels while conserving the ration.
resized_image = image.resize((int(image.size[0] * (length / image.size[1])), length))
# Amount of pixel to lose in total on the width of the image.
required_loss = resized_image.size[0] - length
# Crop the width of the image so as to keep 1080 pixels of the center part.
resized_image = resized_image.crop(
box=(required_loss / 2, 0, resized_image.size[0] - required_loss / 2, length))
# We now have a length*length pixels image.
return resized_image
调整大小的图像示例:
答案 2 :(得分:0)
这是一个Python库,可以帮助您调整图像的大小,同时保持宽高比。如果原始宽高比与您将其转换成的宽高比不同,它将添加具有您选择的颜色的背景。
https://pypi.org/project/python-resize-image/
您可能想看看此功能
resize_contain(image, size, validate=True, resample=Image.LANCZOS, bg_color=(255, 255, 255, 0))
答案 3 :(得分:-1)
IF you are looking to keep the shape of your original image and still make it a square, consider adding padding to make it a square and then resize as Robbie suggests