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 1 mode images to be inverted #6034

Merged
merged 1 commit into from Mar 30, 2022
Merged

Conversation

radarhere
Copy link
Member

Allows autocontrast, equalize, invert, posterize and solarize to support 1 mode images.

Alternative to #6033

@dov
Copy link
Contributor

dov commented Feb 7, 2022

Is this always correct? My special case used the shortcut that if you interpret a 1-bit image as a 8-bit image and apply a linear inverse 8-bit lut on it then it is equivalent to inverting each bit. But this is not valid for a general lut. How exactly does image.point() work for a 1 - bit image?

@radarhere
Copy link
Member Author

if you interpret a 1-bit image as a 8-bit image and apply a linear inverse 8-bit lut on it then it is equivalent to inverting each bit

Following you so far.

But this is not valid for a general lut.

So you're concerned that the methods other than invert won't be correct?

L 1
autocontrast autocontrast_L autocontrast_1
equalize equalize_L equalize_1
invert invert_L invert_1
posterize posterize_L posterize_1
solarize solarize_L solarize_1

@wiredfool
Copy link
Member

I'm not sure there's any point to autocontrast, equalize, posterize or solarize on 1 bit images. They're just fundamentally not 1 bit operations.

@radarhere
Copy link
Member Author

In terms of the code, it is simpler to support those operations than to exclude them.

@radarhere
Copy link
Member Author

How exactly does image.point() work for a 1 - bit image?

I'm not following this part of your question. It works in the same way that it does for an L mode image. A 1 mode image just happens to only have 0 and 255 values, and nothing in the middle.

@wiredfool
Copy link
Member

Yes, but those operations aren't properly defined for 1 bit images. It's valid for invert, but that's it. You're getting the same image back in the other cases (except for solarize).

I think it's wrong to attempt these other transforms on a 1 bit image.

@radarhere radarhere changed the title Support 1 mode images in several ImageOps methods Allow 1 mode images to be inverted Feb 7, 2022
@radarhere
Copy link
Member Author

Ok, I've removed support for the other operations.

@dov
Copy link
Contributor

dov commented Feb 8, 2022

Great! I just tested and your Image.point() version it is actually faster than my translate() implementation. Before testing I thought it might be slower since the documentation for Image.Point() states that it does the lookup one pixel at a time. But that is actually not true for 1-bit images as they are processed the same as L images with the lookup table applying to eight bits at a time. As long as nobody assumes that in Image.point() they can use lookup table of all zero and one 255, "since 1-bit images only have gray levels 0 and 255", we're fine.

(On a C level invert() can be speeded up by 1. getting rid of the redundant lookup table and just doing a bitwise NOT and 2. doing the processing size_t (typically 64 bits) at a time. But I doubt any users of PIL.Image care about that.)

@hugovk hugovk merged commit 4bc6483 into python-pillow:main Mar 30, 2022
@radarhere radarhere deleted the imageops branch March 30, 2022 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants