3

So I have this issue that when someone uploads an image to my Django 1.9.2 server via an iPhone/Smartphone, the image appears rotated whenever it is fetched. I was wondering if there is a way to check the EXIF data if the image is rotated when the form/image is submitted, but rotate it before it is saved into the model.

I am trying this Proposed Solution in the following thread, but I am getting this error:

      File "C:\Users\NAME\PycharmProjects\PROJECT\venv\lib\site-packages\PIL\Image.py", line 628, in __getattr__
    raise AttributeError(name)
AttributeError: _committed

models.py:

class ListingImage(models.model):
    image = models.ImageField()

views.py

def create_listing(request):
    if request.method == 'POST':
        im = request.FILES.get('listing_image', '')

        try:
            image=Image.open(im)
            if hasattr(image, '_getexif'): # only present in JPEGs
                for orientation in ExifTags.TAGS.keys():
                    if ExifTags.TAGS[orientation]=='Orientation':
                        break
                e = image._getexif()       # returns None if no EXIF data
                if e is not None:
                    exif=dict(e.items())
                    orientation = exif.get(orientation, None)
                    if orientation is None: return

                    if orientation == 3:   image = image.transpose(Image.ROTATE_180)
                    elif orientation == 6: image = image.transpose(Image.ROTATE_270)
                    elif orientation == 8: image = image.transpose(Image.ROTATE_90)
        except:
            image = im
        ListingImage.objects.create(image=image)
Community
  • 1
  • 1
Hybrid
  • 6,741
  • 3
  • 25
  • 45

1 Answers1

1

I found this, it seems like you need to load the image like this PIL attribute error

image=Image.open(im)
loadedImage = image.load()

In their documentation you can find the following

open, Opens and identifies the given image file. This is a lazy operation; the function reads the file header, but the actual image data is not read from the file until you try to process the data (call the load method to force loading).

Community
  • 1
  • 1
JOSEFtw
  • 9,781
  • 9
  • 49
  • 67