제갈묘*_*제갈묘 2 python image python-imaging-library
我想将矩形图像转换为正方形图像。但是,我有问题。
我的试用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()
Run Code Online (Sandbox Code Playgroud)
我的错误在这里:
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__
Run Code Online (Sandbox Code Playgroud)
怎么了?
我在做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
Run Code Online (Sandbox Code Playgroud)
调整大小的图像示例:
| 归档时间: |
|
| 查看次数: |
11907 次 |
| 最近记录: |