I'm using django-resized to reduce sizes of images when they are uploaded to AWS S3.
The resizing works fine when the image is wider than its height. However, when image height is bigger than its width then the output image is rotated by 90 degrees and its width is bigger than its height.
models.py
from django_resized import ResizedImageField
class Catch(models.Model):
fish_type = models.CharField("Fish Type", max_length=50, choices=fish_choices, default="Carp")
catch_id = models.AutoField(primary_key=True)
weight = models.DecimalField("Weight", max_digits=5, decimal_places=2)
length = models.DecimalField("Length", max_digits=5, decimal_places=2, blank=True, null=True)
datetime = models.DateTimeField("Catch Time", auto_now=False, auto_now_add=False)
image = ResizedImageField(size=[1080, 1350], quality=95, null=True, blank=True, default="default_img.png", upload_to="catch_images/")
fisherman = models.ForeignKey(Fisherman, on_delete=models.CASCADE)
trip = models.ForeignKey(Trips, on_delete=models.CASCADE)
hookbait_name = models.CharField('Csali megnevezése', max_length=120, blank=True, null=True)
hookbait = models.ForeignKey(HookBait, on_delete=models.SET_NULL, blank=True, null=True)
settings.py
DJANGORESIZED_DEFAULT_KEEP_META = True
DJANGORESIZED_DEFAULT_FORCE_FORMAT = 'JPEG'
DJANGORESIZED_DEFAULT_FORMAT_EXTENSIONS = {'JPEG': ".jpg"}
DJANGORESIZED_DEFAULT_NORMALIZE_ROTATION = True
I only want to reduce sizes of images while they're preserving the aspect ratio.
Thanks in advance for any help.
EDIT!
I managed to solve the problem on my own. I used pillow to resize images but I also had to rotate them based on the orientation tag.
The rigth code is here:
models.py
from PIL import Image, ExifTags
from io import BytesIO
from django.core.files import File
class Catch(models.Model):
fish_type = models.CharField("Fish Type", max_length=50, choices=fish_choices, default="Carp")
catch_id = models.AutoField(primary_key=True)
weight = models.DecimalField("Weight", max_digits=5, decimal_places=2)
length = models.DecimalField("Length", max_digits=5, decimal_places=2, blank=True, null=True)
datetime = models.DateTimeField("Catch Time", auto_now=False, auto_now_add=False)
image = models.ImageField(null=True, blank=True, default="default_img.png", upload_to="catch_images/")
fisherman = models.ForeignKey(Fisherman, on_delete=models.CASCADE)
trip = models.ForeignKey(Trips, on_delete=models.CASCADE)
hookbait_name = models.CharField('Csali megnevezése', max_length=120, blank=True, null=True)
hookbait = models.ForeignKey(HookBait, on_delete=models.SET_NULL, blank=True, null=True)
class Meta:
verbose_name = "Catch"
verbose_name_plural = "Catches"
def save(self, *args, **kwargs):
if self.image:
img = Image.open(BytesIO(self.image.read()))
if hasattr(img, '_getexif'):
exif = img._getexif()
if exif:
for tag, label in ExifTags.TAGS.items():
if label == 'Orientation':
orientation = tag
break
if orientation in exif:
if exif[orientation] == 3:
img = img.rotate(180, expand=True)
elif exif[orientation] == 6:
img = img.rotate(270, expand=True)
elif exif[orientation] == 8:
img = img.rotate(90, expand=True)
img.thumbnail((1080,1080), Image.ANTIALIAS)
output = BytesIO()
img.save(output, format='JPEG', quality=95)
output.seek(0)
self.image = File(output, self.image.name)
return super().save(*args, **kwargs)