I wanted to crop my profile images into squares and reduce their size. So I googled "image resize matrix." None of the results matched my need, so I wrote my own code. It's in python/django. I decided most images of a head have more space on the bottom because of the neck and shoulders. So I cropped heights from the top, instead of from the middle. All of the widths are cropped to the middle. It uses 300 pixels as a maximum. I suppose this might help someone who has a similar task.
I need more points so I can vote on stuff. I use the site all day and get lots of answers, but I can't vote. Which makes me feel guilty.
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)
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)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With