Skip to content

PIL fails to roundtrip some TIFF images #1039

Closed

Description

@anntzer

For some TIFF images, a simple Image.open(path).save(path) fails with the following:

_TIFFVSetField: /path/to/test.tiff: Bad value 2 for "ExtraSamples" tag.
Traceback (most recent call last):
  <elided traceback>
  File "/usr/lib/python3.4/site-packages/PIL/Image.py", line 1685, in save
    save_handler(self, fp, filename)
  File "/usr/lib/python3.4/site-packages/PIL/TiffImagePlugin.py", line 1185, in _save
    e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
  File "/usr/lib/python3.4/site-packages/PIL/Image.py", line 430, in _getencoder
    return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary

The image was created using Qt's QImageWriter (from a QImage.Format_RBG32 image), but it is not clear how to reproduce the issue as a simple blank image created in the same way does not trigger the same issue.

Activity

  1. hugovk commented on Dec 6, 2014

    @hugovk
    Member

    Please can you share an image that reproduces the problem?

  2. wiredfool commented on Dec 6, 2014

    @wiredfool
    Member

    The RGBA line here in TiffInfoPlugin looks like the issue:

        "RGBX": ("RGBX", II, 2, 1, (8, 8, 8, 8), 0),
        "RGBA": ("RGBA", II, 2, 1, (8, 8, 8, 8), 2),
    

    (though, both of these look wrong. RGBX should have ExtraSamples=1, and RGBA should have 0. I think, I'd have to review the spec to make sure.)

  3. anntzer commented on Dec 6, 2014

    @anntzer
    ContributorAuthor

    Indeed, monkeypatching the SAVE_INFO dict apparently solves the issue.

  4. added
    Bug
    on Dec 8, 2014
  5. hugovk commented on Dec 28, 2014

    @hugovk
    Member

    Comment by @wiredfool hoisted from #1045:


    Looking at the spec, my earlier interpretation doesn't appear to be correct:

    ExtraSamples
    
    Description of extra components.
    
    Tag = 338 (152.H)
    Type = SHORT
    N = m
    
    Specifies that each pixel has m extra components whose interpretation is defined
    by one of the values listed below. When this field is used, the SamplesPerPixel
    field has a value greater than the PhotometricInterpretation field suggests.
    For example, full-color RGB data normally has SamplesPerPixel=3. If
    SamplesPerPixel is greater than 3, then the ExtraSamples field describes the
    meaning of the extra samples. If SamplesPerPixel is, say, 5 then ExtraSamples
    will contain 2 values, one for each extra sample.
    
    ExtraSamples is typically used to include non-color information, such as opacity,
    in an image. The possible values for each item in the field's value are:
    
    0 = Unspecified data
    1 = Associated alpha data (with pre-multiplied color)
    2 = Unassociated alpha data
    

    By this spec, the values are correct as they currently are. That doesn't explain your original problem in #1039 .

  6. anntzer commented on Dec 28, 2014

    @anntzer
    ContributorAuthor

    I think I figured it out and my original fix was indeed wrong. I believe it's a nastier bug, specifically

    • ExtraSamples should be a list of values (one per extra sample), rather than a simple value. I fixed this by temporarily adding
            if EXTRASAMPLES in ifd:
                atts[EXTRASAMPLES] = list(ifd[EXTRASAMPLES])
    

    in _save (just after choosing to use libtiff), but this should really be fixed at load time rather than save time.

    Creating a new PR now.

  7. radarhere commented on Oct 28, 2015

    @radarhere
    Member

    Is this resolved now?

  8. radarhere commented on Oct 28, 2015

    @radarhere
    Member

    Testing, I find that it fails for Python 2.9.x and works in both Python 3.0.x and master. I'll close this, and anyone can request a re-open if they feel otherwise.

  9. wiredfool commented on Oct 28, 2015

    @wiredfool
    Member

    Pillow, I think.

    But anyway, this is part of the massive IFD overhaul, so it should be settled.

  10. anntzer commented on Oct 28, 2015

    @anntzer
    ContributorAuthor

    Yup, I forgot what this was exactly but from my earlier descriptions this must have been fixed by the IFD rewrite.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugTIFF

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions