Search code examples
pythonpython-2.7python-imaging-libraryexif

EXIF python PIL return empty dictionary


I can't figure out why _getexif() returns an empty dictionary. I know that the image contains EXIF data as shown by ImageMagic :

>> identify -verbose image.jpg

Properties:
    date:create: 2015-01-12T16:20:26-05:00
    date:modify: 2013-07-01T14:05:08-04:00
    exif:ColorSpace: 1
    ...

exif:Model: PC800 PROFESSIONAL
    exif:ResolutionUnit: 2
    exif:SceneCaptureType: 0
    exif:WhiteBalance: 1
    exif:XResolution: 72/1
    exif:YCbCrPositioning: 2
    exif:YResolution: 72/1
    jpeg:colorspace: 2
    jpeg:sampling-factor: 2x1,1x1,1x1
    signature: e63fcbacdfd031e611b83befaa4a9c8ef6235894da10784a692f90832205ec60
  Profiles:
    Profile-exif: 931 bytes

Now try to read EXIF with python2

img = Image.open('image.jpg')
>>> img
<PIL.JpegImagePlugin.JpegImageFile image.jpg mode=RGB size=2048x1536 at 0x7FEDF2E8A518>
exif = img._getexif()
>>> exif

returns nothing...

I also tried with pyexiv2, same result:

>>> import pyexiv2
>>> metadata = pyexiv2.ImageMetadata(img)
>>> metadata.read()
>>> metadata.exif_keys
[]

Any ideas why I can't read theses exif data into python ? Python 2.7.8 (default, Sep 24 2014, 18:26:21) on Arch Linux.


Solution

  • As an alternative to using _getexif() we can use subprocess.chck_output to run identify and parse the output into a dict:

    from subprocess import check_output
    
    c = check_output("identify -verbose img.jpg".split())
    d = {}
    print(c)
    for line in c.splitlines()[:-1]:
        spl = line.split(":",1)
        k, v = spl
        d[k.lstrip()] = v.strip()
    print(d)
    
    {'Blue': '', 'Border color': 'srgb(223,223,223)', 'Compression': 'JPEG', 'Elapsed time': '0:01.000', 'Artifacts': '', 'Overall': '', 'blue primary': '(0.15,0.06)', 'Dispose': 'Undefined', 'jpeg': 'sampling-factor: 2x2,1x1,1x1', 'red primary': '(0.64,0.33)', 'Interlace': 'JPEG', 'Compose': 'Over', 'Tainted': 'False', 'Filesize': '3.9KB', 'Pixels per second': '0B', 'Units': 'Undefined', 'Chromaticity': '', 'Number pixels': '7.5K', 'Type': 'TrueColor', 'Red': '', 'Channel depth': '', 'blue': '8-bit', 'Background color': 'white', 'Transparent color': 'black', 'verbose': 'true', 'min': '0 (0)', 'Resolution': '72x72', 'Image statistics': '', 'filename': 'img.jpg', 'green primary': '(0.3,0.6)', 'Print size': '1.38889x1.04167', 'Channel statistics': '', 'Class': 'DirectClass', 'red': '8-bit', 'Matte color': 'grey74', 'Page geometry': '100x75+0+0', 'skewness': '-0.284137', 'Geometry': '100x75+0+0', 'max': '255 (1)', 'Colorspace': 'sRGB', 'Depth': '8-bit', 'Green': '', 'date': 'modify: 2015-01-13T19:18:06+00:00', 'User time': '0.000u', 'Rendering intent': 'Perceptual', 'kurtosis': '0.369947', 'Gamma': '0.454545', 'Orientation': 'Undefined', 'Endianess': 'Undefined', 'Image': 'img.jpg', 'Format': 'JPEG (Joint Photographic Experts Group JFIF format)', 'white point': '(0.3127,0.329)', 'Properties': '', 'green': '8-bit', 'Iterations': '0', 'signature': '620e0541b826edb6dc5a2420beaaf697fdf6682ba50f915b8ab767fafbb3b7d2', 'standard deviation': '46.9874 (0.184264)', 'Quality': '85', 'mean': '133.602 (0.52393)'}
    

    If you don't want keys with empty values use if v before adding the key/value