From 350cc702f1e9499d621472f05898a03b493ce52a Mon Sep 17 00:00:00 2001 From: wiredfool Date: Mon, 7 Oct 2013 23:01:15 -0700 Subject: [PATCH] Additional tests for reading and writing Tiff ImageFileDirectories --- Tests/test_file_libtiff.py | 21 ++++++++++- Tests/test_file_tiff_metadata.py | 61 +++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/Tests/test_file_libtiff.py b/Tests/test_file_libtiff.py index 037fc022d..976f97f34 100644 --- a/Tests/test_file_libtiff.py +++ b/Tests/test_file_libtiff.py @@ -92,7 +92,6 @@ def test_g4_write(): assert_equal(reread.size,(500,500)) _assert_noerr(reread) assert_image_equal(reread, rot) - assert_false(orig.tobytes() == reread.tobytes()) def test_adobe_deflate_tiff(): @@ -105,3 +104,23 @@ def test_adobe_deflate_tiff(): assert_no_exception(lambda: im.load()) +def test_write_metadata(): + """ Test metadata writing through libtiff """ + img = Image.open('Tests/images/lena_g4.tif') + f = tempfile('temp.tiff') + img.save(f, tiffinfo = img.tag) + + loaded = Image.open(f) + + original = img.tag.named() + reloaded = loaded.tag.named() + + ignored = ['StripByteCounts', 'RowsPerStrip', 'PageNumber'] + + for tag, value in reloaded.items(): + if tag not in ignored: + assert_equal(original[tag], value, "%s didn't roundtrip" % tag) + + for tag, value in original.items(): + if tag not in ignored: + assert_equal(value, reloaded[tag], "%s didn't roundtrip" % tag) diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py index 8c38a1cb6..9a050e1e3 100644 --- a/Tests/test_file_tiff_metadata.py +++ b/Tests/test_file_tiff_metadata.py @@ -3,11 +3,12 @@ from PIL import Image, TiffImagePlugin, TiffTags tag_ids = dict(zip(TiffTags.TAGS.values(), TiffTags.TAGS.keys())) -# Test writing arbitray metadata into the tiff image directory -# Use case is ImageJ private tags, one numeric, one arbitrary data. -# https://github.com/python-imaging/Pillow/issues/291 - def test_rt_metadata(): + """ Test writing arbitray metadata into the tiff image directory + Use case is ImageJ private tags, one numeric, one arbitrary + data. https://github.com/python-imaging/Pillow/issues/291 + """ + img = lena() textdata = "This is some arbitrary metadata for a text field" @@ -25,3 +26,55 @@ def test_rt_metadata(): assert_equal(loaded.tag[50838], (len(textdata),)) assert_equal(loaded.tag[50839], textdata) +def test_read_metadata(): + img = Image.open('Tests/images/lena_g4.tif') + + known = {'YResolution': ((1207959552, 16777216),), + 'PlanarConfiguration': (1,), + 'BitsPerSample': (1,), + 'ImageLength': (128,), + 'Compression': (4,), + 'FillOrder': (1,), + 'DocumentName': u'lena.g4.tif', + 'RowsPerStrip': (128,), + 'ResolutionUnit': (1,), + 'PhotometricInterpretation': (0,), + 'PageNumber': (0, 1), + 'XResolution': ((1207959552, 16777216),), + 'ImageWidth': (128,), + 'Orientation': (1,), + 'StripByteCounts': (1796,), + 'SamplesPerPixel': (1,), + 'StripOffsets': (8,), + 'Software': u'ImageMagick 6.5.7-8 2012-08-17 Q16 http://www.imagemagick.org'} + + # assert_equal is equivalent, but less helpful in telling what's wrong. + named = img.tag.named() + for tag, value in named.items(): + assert_equal(known[tag], value) + + for tag, value in known.items(): + assert_equal(value, named[tag]) + + +def test_write_metadata(): + """ Test metadata writing through the python code """ + img = Image.open('Tests/images/lena.tif') + + f = tempfile('temp.tiff') + img.save(f, tiffinfo = img.tag) + + loaded = Image.open(f) + + original = img.tag.named() + reloaded = loaded.tag.named() + + ignored = ['StripByteCounts', 'RowsPerStrip', 'PageNumber', 'StripOffsets'] + + for tag, value in reloaded.items(): + if tag not in ignored: + assert_equal(original[tag], value, "%s didn't roundtrip" % tag) + + for tag, value in original.items(): + if tag not in ignored: + assert_equal(value, reloaded[tag], "%s didn't roundtrip" % tag)