gpt4 book ai didi

python - 如何在 Django 中调整图像大小并将图像裁剪为正方形?

转载 作者:太空宇宙 更新时间:2023-11-03 21:17:56 25 4
gpt4 key购买 nike

我想将我的个人资料图像裁剪成正方形并减小其尺寸。所以我用谷歌搜索“图像调整大小矩阵”。结果都不符合我的需要,所以我编写了自己的代码。它在 python/django 中。我认为大多数头部图像的底部都有更多空间,因为脖子和肩膀。所以我从顶部而不是中间裁剪高度。所有宽度都被裁剪到中间。它最多使用 300 像素。我想这可能会对有类似任务的人有所帮助。

我需要更多积分,以便我可以对事物进行投票。我整天使用该网站并得到很多答案,但我无法投票。这让我感到内疚。

from PIL import Image

class CustomUser(AbstractBaseUser, PermissionsMixin):
# User model fields, etc
image = models.ImageField(default='default.jpg',upload_to='profile_pics')

def save(self, *args, **kwargs):
super().save()
img = Image.open(self.image.path)
width, height = img.size # Get dimensions

if width > 300 and height > 300:
# keep ratio but shrink down
img.thumbnail((width, height))
width, height = img.size

# check which one is smaller
if height < width:
# make square by cutting off equal amounts left and right
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
img = img.crop((left, top, right, bottom))
img.thumbnail((300, 300))
img.save(self.image.path)

elif width < height:
# make square by cutting off bottom
left = 0
right = width
top = 0
bottom = width
img = img.crop((left, top, right, bottom))
img.thumbnail((300, 300))
img.save(self.image.path)
else:
# already square
img.thumbnail((300, 300))
img.save(self.image.path)

elif width > 300 and height == 300:
left = (width - 300) / 2
right = (width + 300) / 2
top = 0
bottom = 300
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width > 300 and height < 300:
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width < 300 and height > 300:
# most potential for disaster
left = 0
right = width
top = 0
bottom = width
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width < 300 and height < 300:
if height < width:
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
img = img.crop((left, top, right, bottom))
img.save(self.image.path)
elif width < height:
height = width
left = 0
right = width
top = 0
bottom = height
img = img.crop((left, top, right, bottom))
img.save(self.image.path)
else:
img.save(self.image.path)

elif width == 300 and height > 300:
# potential for disaster
left = 0
right = 300
top = 0
bottom = 300
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width == 300 and height < 300:
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width < 300 and height == 300:
left = 0
right = width
top = 0
bottom = width
img = img.crop((left, top, right, bottom))
img.save(self.image.path)

elif width and height == 300:
img.save(self.image.path)

最佳答案

您重复相同的条件 block 3 次,这使得您的代码难以阅读和维护。

下面的代码与您遵循的过程完全相同,无需重复我提到的内容。

from PIL import Image

class CustomUser(AbstractBaseUser, PermissionsMixin):
# User model fields, etc
image = models.ImageField(default='default.jpg',upload_to='profile_pics')

def save(self, *args, **kwargs):
super().save()
img = Image.open(self.image.path)
width, height = img.size # Get dimensions

if width > 300 and height > 300:
# keep ratio but shrink down
img.thumbnail((width, height))

# check which one is smaller
if height < width:
# make square by cutting off equal amounts left and right
left = (width - height) / 2
right = (width + height) / 2
top = 0
bottom = height
img = img.crop((left, top, right, bottom))

elif width < height:
# make square by cutting off bottom
left = 0
right = width
top = 0
bottom = width
img = img.crop((left, top, right, bottom))

if width > 300 and height > 300:
img.thumbnail((300, 300))

img.save(self.image.path)

关于python - 如何在 Django 中调整图像大小并将图像裁剪为正方形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54545621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com