Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix default crop on images that are rotated in exif #6273

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions bluebottle/files/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.core.files.images import ImageFile
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from sorl.thumbnail import default
from PIL import UnidentifiedImageError, ImageOps

from future.utils import python_2_unicode_compatible

from bluebottle.files.fields import ImageField
Expand Down Expand Up @@ -66,8 +68,15 @@ class JSONAPIMeta(object):

def save(self, *args, **kwargs):
if not self.cropbox and self.file:
image = ImageFile(self.file)
self.cropbox = get_default_cropbox(image, 16 / 9, 40)
self.file.file.seek(0)
try:
image = ImageOps.exif_transpose(
default.engine.get_image(self.file.file)
)

self.cropbox = get_default_cropbox(image, 16 / 9, 40)
except UnidentifiedImageError:
pass

super().save(*args, **kwargs)

Expand Down
13 changes: 10 additions & 3 deletions bluebottle/files/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

from django.db.models import QuerySet
from django.urls import reverse
from django.core.files.images import ImageFile
from rest_framework_json_api import serializers
from rest_framework_json_api.relations import ResourceRelatedField
from rest_framework_json_api.serializers import ModelSerializer

from sorl.thumbnail import default
from PIL import UnidentifiedImageError, ImageOps

from bluebottle.files.models import Document, Image, PrivateDocument
from bluebottle.utils.utils import reverse_signed

Expand Down Expand Up @@ -152,9 +154,14 @@ class ImageSerializer(DocumentSerializer):
def get_size(self, obj):
try:
obj.file.seek(0)
image_file = ImageFile(obj.file)
try:
image_file = ImageOps.exif_transpose(
default.engine.get_image(obj.file)
)

return {'width': image_file.width, 'height': image_file.height}
return {'width': image_file.width, 'height': image_file.height}
except UnidentifiedImageError:
pass
except (FileNotFoundError, AttributeError):
pass

Expand Down