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

Allow saving 1 and L mode TIFF with PhotometricInterpretation 0 #5655

Merged
merged 2 commits into from Aug 14, 2021

Conversation

radarhere
Copy link
Member

Resolves #4804

TIFF tag 262 is PhotometricInterpretation. Pillow currently does not allow this value to be set when saving. Changing the value for mode 1 images means that white becomes black and vice versa, so to keep the output the same, we need to invert the pixel values.

This PR allows for that specific scenario, inverting the values when saving.

px = inverted_im.load()
for y in range(inverted_im.height):
for x in range(inverted_im.width):
px[x, y] = 0 if px[x, y] == 255 else 255
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only handles bilevel, but grayscale (mode "L") images can also have WhiteIsZero interpretation, so might be an idea to cover them as well here (maybe use 255-px[x ,y] and also check for "L" above?)

Copy link
Contributor

@kmilos kmilos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@radarhere radarhere changed the title Allow saving 1 mode TIFF with PhotometricInterpretation 0 Allow saving 1 and L mode TIFF with PhotometricInterpretation 0 Aug 6, 2021
@hugovk hugovk merged commit 2d01f7d into python-pillow:master Aug 14, 2021
@radarhere radarhere deleted the whiteiszero branch August 14, 2021 20:39
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 this pull request may close these issues.

Tag 262 (PhotometricInterpretation) is changed when saving as a TIFF
3 participants