Skip to content

Pillow refuses to write TIFs with unknown tags #983

Closed

Description

@BaconRanch

I work with georeferenced images as TIFs and have never had any problems with standard PIL opening and saving them.

When upgrading our codebase to Pillow a few days ago, lots of stuff broke. In nearly all cases, it comes down to the fact that Pillow is extremely anal about how it saves TIF files. Any misunderstood tags or tags with what it thinks are incorrect values according to the spec will result in exceptions and a refusal to write the file, an issue which PIL never had.

Now, georef'd TIFs contain several nonstandard tags. Here's an example image for you to work with: https://dl.dropboxusercontent.com/u/40538551/FtHood.tif

When working with the image above:

from PIL import Image
img = Image.open('FtHood.tif')
img.save('test.tif')

Pillow produces this output:

TIFFSetField: test.tif: Unknown tag 42112.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/Image.py", line 1685, in save
    save_handler(self, fp, filename)
  File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/TiffImagePlugin.py", line 1185, in _save
    e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
  File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/Image.py", line 430, in _getencoder
    return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary

Notice the TIFFSetField: test.tif: Unknown tag 42112. This error seems to be coming from the included libtiff.so library that Pillow calls out to for the encoding of the TIF when saving the image.

42112 is a metadata tag that GDAL produces when it works with TIFs: http://www.awaresystems.be/imaging/tiff/tifftags/gdal_metadata.html The tag itself is common in georeferenced images, but the key takeaway here is that Pillow throws an exception whenever its included libtiff encounters any unknown tag! Imagemagick, for example, will throw warnings when working with this image, but it still writes output and works with it just fine- no exceptions, no explosions. PIL also works with the image. If I replace Pillow with PIL in my virtual environment, the above 3-liner python code works perfectly.

Here's how to get the above image to save when using Pillow:

from PIL import Image
img = Image.open('FtHood.tif')
del img.tag[339]
del img.tag[42112]
del img.tag[34735]
del img.tag[34737]
del img.tag[34264]
img.save('test.tif')

Notice that I had to delete a whole variety of tags. These are all related to georeferencing (except for the first delete, 339, I'll get to that in a second). So any image with nonstandard tags will have to have an arbitrary number of them deleted before Pillow will actually save the image. As you could imagine, this is very impractical to work with and causes a codebase using Pillow to become filled with random exceptions based on image input. I think it would be better behavior for Pillow to work like PIL and lots of other TIF-handling software: if you don't specifically know what the TIF tag is, just write it to the file anyway. There are so many different tags, it's impossible to understand them all and some software even defines its own application-specific ones.

Now, I also had to delete 339. 339 is the SampleFormat tag: http://www.awaresystems.be/imaging/tiff/tifftags/sampleformat.html and in this case, it's set to Unsigned, Unsigned, Unsigned: one 'Unsigned' for each bit sample. From my understanding, Unsigned is a valid value for the tag and there should be no reason to throw an exception concerning it: _TIFFVSetField: test.tif: Bad value 29680 for "SampleFormat" tag. Just like with the georef tags, vanilla PIL has no problems with SampleFormat in the example image.

Note that I'm using Python 2.7.8 and Pillow 2.6.1

Activity

  1. wiredfool commented on Oct 31, 2014

    @wiredfool
    Member

    This is a duplicate of #660. Essentially, libtiff needs a special handling for tags that it doesn't already know about, which is anything that's not standard.

    PIL didn't run into this issue because it didn't use libtiff, and just blindly wrote the tags. On the other hand, it didn't understand reading most compression methods, nor could it actually write compressed images correctly.

    As for tag 339, 29680 isn't the value for unsigned, 1 is. At any rate, I'd think that it should be set by the save function since it depends on the image mode. It looks like it's defaulting to writing based on the image mode, but it can be overridden. That should probably be fixed as well.

  2. BaconRanch commented on Oct 31, 2014

    @BaconRanch
    Author

    Sorry to duplicate. I should have looked at the open issues more carefully. Any idea on time to fix? I realize it says 'Future' as the milestone for #660.

    I have a couple more issues unrelated to this one and I will look carefully at what's open before posting.

  3. wiredfool commented on Nov 1, 2014

    @wiredfool
    Member

    No worries on the dup -- last time I tried search here I got a 404.

    There is sort of support for tiled tiff images, but not through libtiff. There's an issue for that somewhere ( #672 ) as well. What support there is is certainly not well tested.

    As for schedule, there really isn't one. I'd review and merge (working) fixes for any of the open issues if someone comes up with them, other than that it's when I have time to work on it when it's the top priority.

    There are a bunch of bits in the tiff portion of the code that need looked at, but they're behind at least the jpeg2k/pthread bug and reviewing current pull requests.

    FWIW, the libtiff support essentially grew out of a patch to read group4 compressed images, then gained any compression, and write support. A lot of the cruftiness comes from making it work well on small images and hacking it piece by piece to add other features.

  4. BaconRanch commented on Nov 1, 2014

    @BaconRanch
    Author

    Cool. It's nice that libtiff has awareness of the tags over PIL's solution of dumbly passing the tags through. It seems like a much better solution altogether and I can understand that full TIF support is awaiting a backlog of other stuff. I've been happy with Pillow overall- keep up the good work!

  5. BaconRanch commented on Nov 1, 2014

    @BaconRanch
    Author

    Oh! One last thing (promise)!

    It seems like any TIFF produced by ImageMagick's convert command always causes Pillow to throw an IOError:

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/Image.py", line 1406, in save
        self.load()
      File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/ImageFile.py", line 215, in load
        raise_ioerror(e)
      File "/Users/sam/.virtualenvs/ff/lib/python2.7/site-packages/PIL/ImageFile.py", line 52, in raise_ioerror
        raise IOError(message + " when reading image file")
    IOError: broken data stream when reading image file
    

    I could certainly try investigating this if it's the first you've heard of it. Seems like there's already an issue or at least an awareness of most of these quirks! Anyway, if this is news to you, I can look into it further and post an issue or something.

    Thanks for all the help and responsiveness by the way!

  6. wiredfool commented on Nov 1, 2014

    @wiredfool
    Member

    Re imagamagick and convert -- I'm not aware of that issue. I've used convert a lot, for many of the test images even, and I haven't seen that sort of behavior. I would say that it's probably tickling a feature of tiff that Pillow doesn't support.

  7. jondo commented on Nov 2, 2015

    @jondo

    It seems this issue should be closed, since it duplicates #660.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions