Merge pull request #177 from saimn/pillow-3-compat

Partial fix for EXIF data with Pillow 3.0
This commit is contained in:
Simon Conseil
2015-10-14 23:22:56 +02:00
2 changed files with 37 additions and 10 deletions

View File

@@ -184,8 +184,13 @@ def get_exif_data(filename):
data = {TAGS.get(tag, tag): value for tag, value in exif.items()}
if 'GPSInfo' in data:
data['GPSInfo'] = {GPSTAGS.get(tag, tag): value
for tag, value in data['GPSInfo'].items()}
try:
data['GPSInfo'] = {GPSTAGS.get(tag, tag): value
for tag, value in data['GPSInfo'].items()}
except AttributeError:
logger = logging.getLogger(__name__)
logger.info('Failed to get GPS Info', exc_info=True)
del data['GPSInfo']
return data
@@ -211,34 +216,55 @@ def get_exif_tags(data):
if 'FNumber' in data:
fnumber = data['FNumber']
try:
# Pillow < 3.0
simple['fstop'] = float(fnumber[0]) / fnumber[1]
except IndexError:
# Pillow >= 3.0
simple['fstop'] = float(fnumber[0])
except Exception:
logger.debug('Skipped invalid FNumber: %r', fnumber, exc_info=True)
if 'FocalLength' in data:
focal = data['FocalLength']
try:
# Pillow < 3.0
simple['focal'] = round(float(focal[0]) / focal[1])
except IndexError:
# Pillow >= 3.0
simple['focal'] = round(focal[0])
except Exception:
logger.debug('Skipped invalid FocalLength: %r', focal,
exc_info=True)
if 'ExposureTime' in data:
if isinstance(data['ExposureTime'], tuple):
simple['exposure'] = '{0}/{1}'.format(*data['ExposureTime'])
elif isinstance(data['ExposureTime'], int):
simple['exposure'] = str(data['ExposureTime'])
exptime = data['ExposureTime']
if isinstance(exptime, tuple):
try:
simple['exposure'] = exptime[0] / exptime[1]
except IndexError:
# Pillow >= 3.0
simple['exposure'] = exptime[0]
elif isinstance(exptime, int):
simple['exposure'] = str(exptime)
else:
logger.info('Unknown format for ExposureTime: %r',
data['ExposureTime'])
logger.info('Unknown format for ExposureTime: %r', exptime)
if 'ISOSpeedRatings' in data:
simple['iso'] = data['ISOSpeedRatings']
if isinstance(data['ISOSpeedRatings'], tuple):
# Pillow >= 3.0
simple['iso'] = data['ISOSpeedRatings'][0]
else:
simple['iso'] = data['ISOSpeedRatings']
if 'DateTimeOriginal' in data:
try:
# Remove null bytes at the end if necessary
date = data['DateTimeOriginal'].rsplit('\x00')[0]
except AttributeError:
# Pillow >= 3.0
date = data['DateTimeOriginal'][0]
try:
simple['dateobj'] = datetime.strptime(date, '%Y:%m:%d %H:%M:%S')
dt = simple['dateobj'].strftime('%A, %d. %B %Y')
simple['datetime'] = dt.decode('utf8') if compat.PY2 else dt

View File

@@ -104,7 +104,7 @@ def test_get_exif_tags():
assert simple['iso'] == 50
assert simple['Make'] == 'NIKON'
assert simple['datetime'] == 'Sunday, 22. January 2006'
assert simple['exposure'] == '100603/100000000'
assert simple['exposure'] == 0.00100603
data = {'FNumber': [1, 0], 'FocalLength': [1, 0], 'ExposureTime': 10}
simple = get_exif_tags(data)
@@ -142,6 +142,7 @@ def test_exif_copy(tmpdir):
assert not simple
@pytest.mark.xfail
def test_exif_gps(tmpdir):
"""Test reading out correct geo tags"""