3๐
You repeated the same condition blocks 3 times which makes your code hard to read and maintain.
The code below is the exact same procedure you follow without that repeat I mentioned.
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)
1๐
Try this.
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)
๐คBK94
- [Django]-Django all-auth Form errors not displaying
- [Django]-Context Aware Browsable API Rendering in Django REST
- [Django]-Catch gaierror in Django / Python?
0๐
django-cropper-image is an app for client side cropping and compressing uploaded images via Djangoโs app using with help cropper.js. github link 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)
๐คAneesh Usman
- [Django]-Django ModelForm not validating correctly with custom date format
- [Django]-Populate json to html with django template
- [Django]-How to get string representation of PrimaryKeyRelatedField in JSON
- [Django]-What is diffrent between WSGIRequest and django HttpRequest?
- [Django]-Change the location of models.py with south
-1๐
You can use Pillow to manipulate images in python. You can install it using pip. It has a lot of features which can help you in manipulating images. You can read more about Pillow here:
๐คharriebird
- [Django]-How to use non valid identifiers as django form field names
- [Django]-Increasing number of initial forms in a Django formset based on data in POST?
- [Django]-Django query with contains and keywords
- [Django]-Image is not uploaded via django form
Source:stackexchange.com