2014-09-05 13:14:45 +04:00
|
|
|
from helper import unittest, PillowTestCase, hopper
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2013-10-03 09:06:17 +04:00
|
|
|
from PIL import Image, TiffImagePlugin, TiffTags
|
|
|
|
|
|
|
|
tag_ids = dict(zip(TiffTags.TAGS.values(), TiffTags.TAGS.keys()))
|
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestFileTiffMetadata(PillowTestCase):
|
|
|
|
|
|
|
|
def test_rt_metadata(self):
|
2014-08-26 16:51:37 +04:00
|
|
|
""" Test writing arbitrary metadata into the tiff image directory
|
2014-06-10 13:10:47 +04:00
|
|
|
Use case is ImageJ private tags, one numeric, one arbitrary
|
|
|
|
data. https://github.com/python-pillow/Pillow/issues/291
|
|
|
|
"""
|
|
|
|
|
2014-09-05 13:14:45 +04:00
|
|
|
img = hopper()
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
textdata = "This is some arbitrary metadata for a text field"
|
|
|
|
info = TiffImagePlugin.ImageFileDirectory()
|
|
|
|
|
|
|
|
info[tag_ids['ImageJMetaDataByteCounts']] = len(textdata)
|
|
|
|
info[tag_ids['ImageJMetaData']] = textdata
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
f = self.tempfile("temp.tif")
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
img.save(f, tiffinfo=info)
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
loaded = Image.open(f)
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assertEqual(loaded.tag[50838], (len(textdata),))
|
|
|
|
self.assertEqual(loaded.tag[50839], textdata)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_read_metadata(self):
|
2014-09-23 11:30:55 +04:00
|
|
|
img = Image.open('Tests/images/hopper_g4.tif')
|
2013-10-03 09:06:17 +04:00
|
|
|
|
2014-09-23 11:40:03 +04:00
|
|
|
known = {'YResolution': ((4294967295, 113653537),),
|
2014-06-10 13:10:47 +04:00
|
|
|
'PlanarConfiguration': (1,),
|
|
|
|
'BitsPerSample': (1,),
|
|
|
|
'ImageLength': (128,),
|
|
|
|
'Compression': (4,),
|
|
|
|
'FillOrder': (1,),
|
|
|
|
'RowsPerStrip': (128,),
|
2014-09-23 11:30:55 +04:00
|
|
|
'ResolutionUnit': (3,),
|
2014-06-10 13:10:47 +04:00
|
|
|
'PhotometricInterpretation': (0,),
|
|
|
|
'PageNumber': (0, 1),
|
2014-09-23 11:40:03 +04:00
|
|
|
'XResolution': ((4294967295, 113653537),),
|
2014-06-10 13:10:47 +04:00
|
|
|
'ImageWidth': (128,),
|
|
|
|
'Orientation': (1,),
|
2014-09-23 11:30:55 +04:00
|
|
|
'StripByteCounts': (1968,),
|
2014-06-10 13:10:47 +04:00
|
|
|
'SamplesPerPixel': (1,),
|
|
|
|
'StripOffsets': (8,),
|
2014-09-23 11:30:55 +04:00
|
|
|
}
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# self.assertEqual is equivalent,
|
|
|
|
# but less helpful in telling what's wrong.
|
|
|
|
named = img.tag.named()
|
|
|
|
for tag, value in named.items():
|
|
|
|
self.assertEqual(known[tag], value)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for tag, value in known.items():
|
|
|
|
self.assertEqual(value, named[tag])
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_write_metadata(self):
|
|
|
|
""" Test metadata writing through the python code """
|
2014-09-05 13:14:45 +04:00
|
|
|
img = Image.open('Tests/images/hopper.tif')
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
f = self.tempfile('temp.tiff')
|
|
|
|
img.save(f, tiffinfo=img.tag)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
loaded = Image.open(f)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
original = img.tag.named()
|
|
|
|
reloaded = loaded.tag.named()
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
ignored = [
|
|
|
|
'StripByteCounts', 'RowsPerStrip', 'PageNumber', 'StripOffsets']
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for tag, value in reloaded.items():
|
|
|
|
if tag not in ignored:
|
|
|
|
self.assertEqual(
|
|
|
|
original[tag], value, "%s didn't roundtrip" % tag)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
for tag, value in original.items():
|
|
|
|
if tag not in ignored:
|
|
|
|
self.assertEqual(
|
|
|
|
value, reloaded[tag], "%s didn't roundtrip" % tag)
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-08-26 16:51:37 +04:00
|
|
|
def test_no_duplicate_50741_tag(self):
|
|
|
|
self.assertEqual(tag_ids['MakerNoteSafety'], 50741)
|
|
|
|
self.assertEqual(tag_ids['BestQualityScale'], 50780)
|
|
|
|
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2013-10-08 10:01:15 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# End of file
|