Visitar URL original
Allow saving 1 and L mode TIFF with PhotometricInterpretation 0 by radarhere · Pull Request #5655 · python-pillow/Pillow · GitHub
Skip to content

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

Merged
hugovk merged 2 commits into
python-pillow:masterfrom
radarhere:whiteiszero
Aug 14, 2021
Merged

hugovk merged 2 commits into
python-pillow:masterfrom
radarhere:whiteiszero

Conversation

@radarhere

Copy link
Copy Markdown
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.

Comment thread src/PIL/TiffImagePlugin.py Outdated
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
Copy Markdown
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?)

@kmilos kmilos left a comment

Copy link
Copy Markdown
Contributor

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