diff --git a/PIL/TiffImagePlugin.py b/PIL/TiffImagePlugin.py index eec187985..ffb1e81bd 100644 --- a/PIL/TiffImagePlugin.py +++ b/PIL/TiffImagePlugin.py @@ -217,7 +217,7 @@ class ImageFileDirectory(collections.MutableMapping): # represents a TIFF tag directory. to speed things up, # we don't decode tags unless they're asked for. - def __init__(self, prefix): + def __init__(self, prefix=II): self.prefix = prefix[:2] if self.prefix == MM: self.i16, self.i32 = ib16, ib32 @@ -456,8 +456,15 @@ class ImageFileDirectory(collections.MutableMapping): data = value = b"".join(value) elif isinstance(value[0], str): # string data + if isinstance(value, tuple): + value = value[-1] typ = 2 - data = value = b"\0".join(value.encode('ascii', 'replace')) + b"\0" + # was b'\0'.join(str), which led to \x00a\x00b sorts + # of strings which I don't see in in the wild tiffs + # and doesn't match the tiff spec: 8-bit byte that + # contains a 7-bit ASCII code; the last byte must be + # NUL (binary zero). + data = value = b"".join(value.encode('ascii', 'replace')) + b"\0" else: # integer data if tag == STRIPOFFSETS: @@ -929,6 +936,18 @@ def _save(im, fp, filename): ifd[IMAGEWIDTH] = im.size[0] ifd[IMAGELENGTH] = im.size[1] + # write any arbitrary tags passed in as an ImageFileDirectory + info = im.encoderinfo.get("tiffinfo",{}) + if Image.DEBUG: + print ("Tiffinfo Keys: %s"% info.keys) + for key in info.keys(): + ifd[key] = info.get(key) + try: + ifd.tagtype[key] = info.tagtype[key] + except: + pass # might not be an IFD, Might not have populated type + + # additions written by Greg Couch, gregc@cgl.ucsf.edu # inspired by image-sig posting from Kevin Cazabon, kcazabon@home.com if hasattr(im, 'tag'): @@ -937,12 +956,6 @@ def _save(im, fp, filename): if key in im.tag.tagdata: ifd[key] = im.tag.tagdata.get(key) - info = im.encoderinfo.get("tiffinfo",[]) - print("info %s "% info) - for key in info: - if key in im.tag: - ifd[key] = im.tag.get(key) - print ("added %s" %key) # preserve some more tags from original TIFF image file # -- 2008-06-06 Florian Hoech ifd.tagtype = im.tag.tagtype diff --git a/Tests/test_file_tiff_metadata.py b/Tests/test_file_tiff_metadata.py new file mode 100644 index 000000000..8c38a1cb6 --- /dev/null +++ b/Tests/test_file_tiff_metadata.py @@ -0,0 +1,27 @@ +from tester import * +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(): + img = lena() + + textdata = "This is some arbitrary metadata for a text field" + info = TiffImagePlugin.ImageFileDirectory() + + info[tag_ids['ImageJMetaDataByteCounts']] = len(textdata) + info[tag_ids['ImageJMetaData']] = textdata + + f = tempfile("temp.tif") + + img.save(f, tiffinfo=info) + + loaded = Image.open(f) + + assert_equal(loaded.tag[50838], (len(textdata),)) + assert_equal(loaded.tag[50839], textdata) +