Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tag 262 (PhotometricInterpretation) is changed when saving as a TIFF #4804

Closed
eliu-alltrust opened this issue Jul 20, 2020 · 3 comments · Fixed by #5655
Closed

Tag 262 (PhotometricInterpretation) is changed when saving as a TIFF #4804

eliu-alltrust opened this issue Jul 20, 2020 · 3 comments · Fixed by #5655
Labels

Comments

@eliu-alltrust
Copy link

What did you do?

save an uncompressed tiff file to group 4 compressed tiff file

What did you expect to happen?

I expected to get a group 4 compressed tiff file and kept all other tags the same as original file

What actually happened?

I got a group 4 compressed tiff file, but Tag 262 (PhotometricInterpretation) was changed from 0 to 1

What are your OS, Python and Pillow versions?

  • OS: Window 10
  • Python: Python 3.6.8
  • Pillow: 7.2.0
from PIL import Image

img = Image.open('uncompressed.tiff')
print(img.tag)
img.save('group4.tiff', compression="group4", tiffinfo=img.tag)
img = Image.open('group4.tiff')
print(img.tag)

pillowIssue.zip

@radarhere radarhere added the TIFF label Jul 21, 2020
@radarhere radarhere changed the title The Tag 262 (PhotometricInterpretation) for the saved tiff file is alway 1 The Tag 262 (PhotometricInterpretation) for the saved tiff file is always 1 Jul 31, 2020
@radarhere radarhere changed the title The Tag 262 (PhotometricInterpretation) for the saved tiff file is always 1 Tag 262 (PhotometricInterpretation) for the saved tiff file is always 1 Nov 5, 2020
@radarhere
Copy link
Member

As an explanation of what is happening, the image you have provided has mode 1. This is how we are setting the photometric interpretation when saving.

rawmode, prefix, photo, format, bits, extra = SAVE_INFO[im.mode]

SAVE_INFO = {
# mode => rawmode, byteorder, photometrics,
# sampleformat, bitspersample, extra
"1": ("1", II, 1, 1, (1,), None),

So depending on the mode, it will not always be 1. But I agree that we're not using the same value that was set.

It controls "the color space of the image data". This is not a random tag that we've just missed - if we were to just flip a switch and set it to 0, it would invert the colors for this case. We could invert the pixels correspondingly, but the value of doing so is not clear - is there any reason you would like it to be the same?

@radarhere radarhere changed the title Tag 262 (PhotometricInterpretation) for the saved tiff file is always 1 Tag 262 (PhotometricInterpretation) is changed when saving as a TIFF Apr 29, 2021
@TebanoE
Copy link

TebanoE commented Jul 21, 2021

I am facing the same problem as eliu-alltrust and I think it is a bug in pillow.

The reason for the existence of tag 262 is that there is no general agreement on how values in 1-bit images are interpreted. "1" can be seen as "high intensity" = white or as "set a point" = black. Tag 262 specifies which of those two interpretations should be used when reading a file, not whether pixels are to be inverted when writing a file.

However, the later is what happens in pillow: I changed the hard coded photometrics value in TiffImagePlugin.py line 1421 to 0 and pillow inverted my image data when saving. It should, however, leave the pixels unchanged regardless of photometrics and write tag 262 with the value specified by the user.

@radarhere
Copy link
Member

So there is a specific need for tag 262 to be set to zero for 1-bit images? Ok then, I have created PR #5655 to resolve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants