From c45c616a60255301e4593397498955d6049ae6a4 Mon Sep 17 00:00:00 2001 From: John Evans Date: Fri, 7 Jun 2013 08:50:00 -0400 Subject: [PATCH] Fixed #26, fixed #36 --- CHANGES.txt | 3 +++ glymur/jp2box.py | 10 ++++++++-- glymur/test/test_jp2k.py | 33 +++++++++++++++++++++++++++++++++ glymur/test/test_printing.py | 19 ++++++++++--------- 4 files changed, 54 insertions(+), 11 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 056f3a9..63ca85d 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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. diff --git a/glymur/jp2box.py b/glymur/jp2box.py index 789c803..e8e7678 100644 --- a/glymur/jp2box.py +++ b/glymur/jp2box.py @@ -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 diff --git a/glymur/test/test_jp2k.py b/glymur/test/test_jp2k.py index a32cda8..5e9ab9b 100644 --- a/glymur/test/test_jp2k.py +++ b/glymur/test/test_jp2k.py @@ -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('