[英]Python PIL - Resize Images
I'm trying to change the size of an image, turning larger images into smaller images and vice versa, with PIL.我正在尝试使用 PIL 更改图像的大小,将较大的图像转换为较小的图像,反之亦然。 I have done various tests, but I cannot get the result I want.
我做了各种测试,但我无法得到我想要的结果。 Here are the tests I did with a 900x1600 image:
以下是我对 900x1600 图像所做的测试:
Same input every time: Image每次输入相同:图像
size = (1080, 1080)
userImage = Image.open("./Images/UsersImages/001.png")
userImage.resize(size)
userImage.show()
size = (1080, 1080)
userImage = Image.open("./Images/UsersImages/001.png")
userImage.thumbnail(size, Image.ANTIALIAS)
userImage.show()
userImage = Image.open("./Images/UsersImages/001.png")
userImageWidth, userImageHeight = userImage.size
leftBorder = 4
topBorder = userImageHeight / 5
rightBorder = 154
bottomBorder = 3 * userImageHeight / 5
userImage = userImage.crop((leftBorder, topBorder, rightBorder, bottomBorder))
newsize = (1080, 1080)
userImage = userImage.resize(newsize)
userImage.show()
userImage = Image.open("./Images/UsersImages/001.png")
userImageWidth, userImageHeight = userImage.size
if userImageWidth > 1080:
bottomBorder = (userImageWidth - 1080) / 2
topBorder = bottomBorder
else:
bottomBorder, topBorder = 0, 0
if userImageHeight > 1080:
leftBorder = (userImageHeight - 1080) / 2
rightBorder = leftBorder
else:
leftBorder, rightBorder = 0, 0
userImage = userImage.crop((leftBorder, topBorder, rightBorder, bottomBorder))
userImage.show()
In most cases, the photo remains exactly the same as before.在大多数情况下,照片与以前完全相同。 In cases with crop, it is cut very small.
在作物的情况下,它被切得很小。
How can I resize any image to 1080x1080?如何将任何图像调整为 1080x1080? I don't care if the photo is stretched.
我不在乎照片是否被拉伸。 The important thing is that any type of image, whether larger or smaller, is resized to 1080x1080.
重要的是,任何类型的图像,无论大小,都被调整为 1080x1080。
Use userImage = userImage.resize(size)
, because resize()
returns a copy of the image, resized;使用
userImage = userImage.resize(size)
,因为resize()
返回图像的副本,resized; it doesn't actively resize the image.它不会主动调整图像大小。
size = (1080, 1080)
userImage = Image.open(f"./Images/UsersImages/001.png")
userImage = userImage.resize(size) ### EDITED LINE
userImage.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.