Fixed #26, fixed #36

This commit is contained in:
John Evans 2013-06-07 08:50:00 -04:00
commit c45c616a60
4 changed files with 54 additions and 11 deletions

View file

@ -1,3 +1,6 @@
Jun 07, 2013 - Changed Exif dictionary names from ['Exif', 'Photo', 'Iop',
'GPSInfo'] to ['Image', 'Photo', 'Iop', 'GPSInfo'].
Jun 06, 2013 - v0.1.6 Exif classes made private. Refactored IFD post
processing. Corrected omission of Exif in UUIDBox docstring.

View file

@ -1626,7 +1626,7 @@ class UUIDBox(Jp2kBox):
elif kwargs['uuid'].bytes == b'JpgTiffExif->JP2':
e = Exif(buffer)
d = {}
d['Exif'] = e.exif_image
d['Image'] = e.exif_image
d['Photo'] = e.exif_photo
d['GPSInfo'] = e.exif_gpsinfo
d['Iop'] = e.exif_iop
@ -1779,7 +1779,13 @@ class _Ifd:
def post_process(self, tagnum2name):
for tag, value in self.raw_ifd.items():
tag_name = tagnum2name[tag]
try:
tag_name = tagnum2name[tag]
except KeyError:
# Ok, we don't recognize this tag. Just use the numeric id.
msg = 'Unrecognized Exif tag "{0}".'.format(tag)
warnings.warn(msg, UserWarning)
tag_name = tag
self.processed_ifd[tag_name] = value

View file

@ -4,6 +4,7 @@ import doctest
import imp
import os
import re
import shutil
import struct
import sys
import tempfile
@ -658,5 +659,37 @@ class TestJp2k(unittest.TestCase):
attr_value = elt.attrib['{0}CreatorTool'.format(ns1)]
self.assertEqual(attr_value, 'glymur')
def test_unrecognized_exif_tag(self):
# An unrecognized exif tag should be handled gracefully.
with tempfile.NamedTemporaryFile(suffix='.jp2') as tfile:
shutil.copyfile(self.jp2file, tfile.name)
# The Exif UUID starts at byte 77. There are 8 bytes for the L and
# T fields, then 16 bytes for the UUID identifier, then 6 exif
# header bytes, then 8 bytes for the TIFF header, then 2 bytes
# the the Image IFD number of tags, where we finally find the first
# tag, "Make" (271). We'll corrupt it by changing it into 171,
# which does not correspond to any known Exif Image tag.
with open(tfile.name, 'r+b') as fp:
fp.seek(117)
buffer = struct.pack('<H', int(171))
fp.write(buffer)
# Verify that a warning is issued, but only on python3.
# On python2, just suppress the warning.
if sys.hexversion < 0x03030000:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
j = Jp2k(tfile.name)
else:
with self.assertWarns(UserWarning) as cw:
j = Jp2k(tfile.name)
exif = j.box[3].data
# Were the tag == 271, 'Make' would be in the keys instead.
self.assertTrue(171 in exif['Image'].keys())
self.assertFalse('Make' in exif['Image'].keys())
if __name__ == "__main__":
unittest.main()

View file

@ -820,15 +820,7 @@ class TestPrinting(unittest.TestCase):
lines = ["UUID Box (uuid) @ (77, 638)",
" UUID: 4a706754-6966-6645-7869-662d3e4a5032 (Exif)",
" UUID Data: ",
"{'Exif': {'ExifTag': 138,",
" 'GPSTag': 354,",
" 'Make': 'HTC',",
" 'Model': 'HTC Glacier',",
" 'ResolutionUnit': 2,",
" 'XResolution': 72.0,",
" 'YCbCrPositioning': 1,",
" 'YResolution': 72.0},",
" 'GPSInfo': {'GPSAltitude': 0.0,",
"{'GPSInfo': {'GPSAltitude': 0.0,",
" 'GPSAltitudeRef': 0,",
" 'GPSDateStamp': '2013:02:09',",
" 'GPSLatitude': [42.0, 20.0, 33.61],",
@ -853,6 +845,14 @@ class TestPrinting(unittest.TestCase):
" 75),",
" 'GPSTimeStamp': [19.0, 47.0, 53.0],",
" 'GPSVersionID': (2, 2, 0)},",
" 'Image': {'ExifTag': 138,",
" 'GPSTag': 354,",
" 'Make': 'HTC',",
" 'Model': 'HTC Glacier',",
" 'ResolutionUnit': 2,",
" 'XResolution': 72.0,",
" 'YCbCrPositioning': 1,",
" 'YResolution': 72.0},",
" 'Iop': None,",
" 'Photo': {'ColorSpace': 1,",
" 'ComponentsConfiguration': (1, 2, 3, 0),",
@ -867,6 +867,7 @@ class TestPrinting(unittest.TestCase):
" 'PixelYDimension': 1424}}"]
expected = '\n'.join(lines)
self.assertEqual(actual, expected)
if __name__ == "__main__":