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

问题描述 投票:1回答:2

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

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

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)
python django python-imaging-library crop image-resizing
2个回答
0
投票

您可以使用Pillow在python中操作图像。您可以使用pip安装它。它具有很多功能,可以帮助您处理图像。您可以在这里阅读有关枕头的更多信息:

https://pypi.org/project/Pillow/

https://pillow.readthedocs.io/en/stable/


0
投票

您重复了相同的条件块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)
© www.soinside.com 2019 - 2024. All rights reserved.