Problem with EXIF in some files #518

mpyatishev opened this Issue Feb 5, 2014 · 12 comments


None yet

10 participants


I've got a strange problem with some files with EXIF in them. Here is a traceback and the file in attachment.

Internal Server Error: /place/content/upload/
Traceback (most recent call last):
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/core/handlers/", line 114, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/views/generic/", line 69, in view
    return self.dispatch(request, *args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/utils/", line 29, in _wrapper
    return bound_func(*args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/contrib/auth/", line 22, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/utils/", line 25, in bound_func
    return func(self, *args2, **kwargs2)
  File "/var/www/tourism/www/tourism/core/", line 13, in dispatch
    return super(LoginRequredMixin, self).dispatch(*args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/views/generic/", line 87, in dispatch
    return handler(request, *args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/views/generic/", line 205, in post
    return super(BaseCreateView, self).post(request, *args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/views/generic/", line 171, in post
    return self.form_valid(form)
  File "/var/www/tourism/www/tourism/apps/place/", line 368, in form_valid
  File "/var/www/tourism/www/tourism/apps/place/", line 409, in save
    super(Photo, self).save(*args, **kwargs)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/db/models/", line 545, in save
    force_update=force_update, update_fields=update_fields)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/db/models/", line 582, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/django/dispatch/", line 185, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/var/www/tourism/www/tourism/apps/place/", line 108, in do_post_save
  File "/var/www/tourism/www/tourism/apps/place/", line 101, in rename_files
  File "/var/www/tourism/www/tourism/apps/place/", line 415, in save
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/", line 8, in get_thumbnail
    return default.backend.get_thumbnail(file_, geometry_string, **options)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/", line 61, in get_thumbnail
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/", line 86, in _create_thumbnail
    image = default.engine.create(source_image, geometry, options)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/engines/", line 15, in create
    image = self.orientation(image, geometry, options)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/engines/", line 26, in orientation
    return self._orientation(image)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/sorl/thumbnail/engines/", line 29, in _orientation
    exif = image._getexif()
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/PIL/", line 362, in _getexif
    return _getexif(self)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/PIL/", line 386, in _getexif
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/PIL/", line 421, in load
    tag, typ = i16(ifd), i16(ifd, 2)
  File "/var/www/tourism/env/local/lib/python2.7/site-packages/PIL/", line 29, in i16le
    return i8(c[o]) | (i8(c[o+1])<<8)
IndexError: string index out of range


Pillow member

It looks like the exif info is bad.

(vpy27)erics:~/exif$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> im ='bad_exif.jpg')
>>> im._getexif()
/home/erics/vpy27/local/lib/python2.7/site-packages/Pillow-2.3.0-py2.7-linux-x86_64.egg/PIL/ UserWarning: Possibly corrupt EXIF data.  Expecting to read 19660800 bytes but only got 0. Skipping tag 0
/home/erics/vpy27/local/lib/python2.7/site-packages/Pillow-2.3.0-py2.7-linux-x86_64.egg/PIL/ UserWarning: Possibly corrupt EXIF data.  Expecting to read 2684485632 bytes but only got 0. Skipping tag 0
/home/erics/vpy27/local/lib/python2.7/site-packages/Pillow-2.3.0-py2.7-linux-x86_64.egg/PIL/ UserWarning: Possibly corrupt EXIF data.  Expecting to read 1575056 bytes but only got 785. Skipping tag 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-x86_64/egg/PIL/", line 362, in _getexif
  File "build/bdist.linux-x86_64/egg/PIL/", line 386, in _getexif
  File "build/bdist.linux-x86_64/egg/PIL/", line 421, in load
  File "build/bdist.linux-x86_64/egg/PIL/", line 29, in i16le
IndexError: string index out of range

Imagemagick shows some odd values, but doesn't complain too loudly:

(vpy27)erics@builder-1204-x64:~/exif$ identify -verbose bad_exif.jpg 
Image: bad_exif.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Class: DirectClass
  Geometry: 900x697+0+0
    date:create: 2014-02-05T09:00:12-08:00
    date:modify: 2014-02-05T04:44:16-08:00
    exif:ApertureValue: 0/1000000
    exif:BrightnessValue: 0/1000000
    exif:ColorSpace: 1
    exif:CompressedBitsPerPixel: 0/1000000
    exif:DateTime: 2010:09:11 09:25:01
    exif:ExifImageLength: 697
    exif:ExifImageWidth: 900
    exif:ExifOffset: 314
    exif:ExifVersion: 48, 50, 49, 48
    exif:ExposureBiasValue: 0/1000000
    exif:ExposureIndex: 0/1000000
    exif:ExposureProgram: 0
    exif:ExposureTime: 0/1000000
    exif:FileSource: 0
    exif:Flash: 0
    exif:FNumber: 0/1000000
    exif:FocalLength: 0/1000000
    exif:FocalPlaneResolutionUnit: 0
    exif:FocalPlaneXResolution: 0/1000000
    exif:FocalPlaneYResolution: 0/1000000
    exif:ISOSpeedRatings: 0, 0
    exif:LightSource: 0
    exif:MaxApertureValue: 0/1000000
    exif:MeteringMode: 0
    exif:Orientation: 1
    exif:PrimaryChromaticities: 0/1000000, 0/1000000, 0/1000000, 0/1000000, 0/1000000, 0/1000000
    exif:ReferenceBlackWhite: 0/1000000, 0/1000000, 0/1000000, 0/1000000, 0/1000000, 0/1000000
    exif:ResolutionUnit: 2
    exif:SceneType: 0
    exif:SensingMethod: 0
    exif:ShutterSpeedValue: 0/1000000
    exif:Software: Adobe Photoshop CS5 Macintosh
    exif:SubjectDistance: 0/1000000
    exif:WhitePoint: 0/1000000, 0/1000000
    exif:XResolution: 300/1
    exif:YCbCrCoefficients: 0/1000000, 0/1000000, 0/1000000
    exif:YCbCrPositioning: 0
    exif:YResolution: 300/1
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x2,1x1,1x1
    signature: 2cec441eea9a21e08693067e1ae4e102464c2cedb7b4b2ae5a63733802e3f12a
@yetanotherape yetanotherape referenced this issue in mariocesar/sorl-thumbnail Apr 10, 2014

IndexError while thumbnailing image with bad EXIF #257


I have practically same problem.

  File "/home/project_name/.virtualenvs/project_name/local/lib/python2.7/site-packages/PIL/", line 366, in _getexif
    return _getexif(self)

  File "/home/project_name/.virtualenvs/project_name/local/lib/python2.7/site-packages/PIL/", line 400, in _getexif

  File "/home/project_name/.virtualenvs/project_name/local/lib/python2.7/site-packages/PIL/", line 421, in load
    tag, typ = i16(ifd), i16(ifd, 2)

  File "/home/project_name/.virtualenvs/project_name/local/lib/python2.7/site-packages/PIL/", line 36, in i16le
    return i8(c[o]) | (i8(c[o+1])<<8)

IndexError: string index out of range

I think this issue should be reopened as clearly images with bad EXIF do exist in real life and other applications deal with them gracefully.

Also, #635 may well be a duplicate.

@aclark4life aclark4life reopened this Apr 27, 2014
@nrsimha nrsimha referenced this issue in matthewwithanm/pilkit May 17, 2014

Avoid Transpose to crash when exif data are invalid. #12

@aclark4life aclark4life added the Bug label Jun 7, 2014
@aclark4life aclark4life added this to the Future milestone Jun 7, 2014

The problem is that Pillow uses the "highly experimental" method of extracting EXIF information: PIL.JpegImagePlugin._getexif(). That method has been in PIL since 1.1.4b1. I have no access to the original SVN repository, so I can't say for sure how old _getexif() is. Anyway, _getexif() has been in the "likely to be replaced with something better in a future version" state for quite a long time.

The quick-and-dirty solution would be:

--- a/PIL/
+++ b/PIL/
@@ -377,7 +377,10 @@ class JpegImageFile(ImageFile.ImageFile):
         self.tile = []

     def _getexif(self):
-        return _getexif(self)
+        try:
+            return _getexif(self)
+        except IndexError:
+            return None

 def _getexif(self):

The more sophisticated one is to rewrite _getexif() completely. The new _getexif() should not import TiffImagePlugin and should be tested thoroughly.

Which solution do you prefer, Alex?


Related: #520 (proposal to make an EXIF plugin).


@kkoroviev It would be nice, like Imagemagick and IrfanView, to show what EXIF can be extracted.

But if that's not a trivial fix, I vote for the quick-and-dirty solution until #520 is properly implemented.


Are there any plans to integrate the quick-and-dirty solution in a near future release?
At the moment faulty images are not generated at all or IndexError us thrown in debug mode. So either i patch or try to fix all faulty exif data.
Thank you for your ideas.

Pillow member

@kkoroviev Not sure if you were asking me, but I agree with you: #520 should probably go in the next release unless the "real" fix is trivial. Would be nice to finally address something that is "likely to be replaced with something better in a future version". Now is the future. 😄

@wiredfool wiredfool added the Tiff label Nov 27, 2014

I think that the latest problem in Issue #1163 may have actually been the same problem as this, just with the new Python _binary methods, as PR #1256 means that the bug in this issue is now dealt with gracefully, rather than generating an error.

I would say that this issue can be closed, since #520 seems to have taken over the task of creating a better way of handling EXIF data.


This file


failed with SyntaxError in ._getexif().

ImageMagick read it successfully.


I can't replicate that problem, either with current Pillow or 2.9.x, which would have been the version when you posted this image. Can you provide any information about your platform/version of Pillow/version of Python?

@james2432 james2432 referenced this issue in openstreetview/upload-scripts Aug 1, 2016

Error uploading photos by exif #5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment