簡體   English   中英

如何在 Django 中調整圖像大小並將其裁剪為正方形?

[英]How to resize and crop an image into a square in Django?

我想將我的個人資料圖像裁剪成正方形並縮小它們的大小。 所以我用谷歌搜索“圖像調整大小矩陣”。 沒有一個結果符合我的需要,所以我編寫了自己的代碼。 它在 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)

您重復了 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)

嘗試這個。

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)

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)

您可以使用 Pillow 在 python 中操作圖像。 您可以使用 pip 安裝它。 它有很多功能可以幫助您處理圖像。 您可以在此處閱讀有關 Pillow 的更多信息:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM