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

时间:2019-02-06 01:53:38

标签: python django python-imaging-library crop image-resizing

我想将个人资料图像裁剪为正方形并减小其尺寸。所以我用谷歌搜索了“图像调整矩阵”。没有任何结果符合我的需要,因此我编写了自己的代码。在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)

4 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分: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)

答案 2 :(得分:0)

尝试一下。

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.user_profile_img.path)

        # When image height is greater than its width
        if img.height > img.width:
            # make square by cutting off equal amounts top and bottom
            left = 0
            right = img.width
            top = (img.height - img.width)/2
            bottom = (img.height + img.width)/2
            img = img.crop((left, top, right, bottom))
            # Resize the image to 300x300 resolution
            if img.height > 300 or img.width >300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.user_profile_img.path)

        # When image width is greater than its height
        elif img.width > img.height:
            # make square by cutting off equal amounts left and right
            left = (img.width - img.height)/2
            right = (img.width + img.height)/2
            top = 0
            bottom = img.height
            img = img.crop((left, top, right, bottom))
            # Resize the image to 300x300 resolution
            if img.height > 300 or img.width >300:
                output_size = (300, 300)
                img.thumbnail(output_size)
                img.save(self.user_profile_img.path)

答案 3 :(得分:0)

django-cropper-image 是一个客户端裁剪和压缩上传图像的应用程序,通过 Django 的应用程序使用帮助cropper.js。 github 链接 django-cropper-image

from django.db import models
from django_cropper_image.fields import ImageCropperField
class Images(models.Model):
    image = ImageCropperField(upload_to='image',max_length=255)