Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ANI: Reduce Pillow frames to RGB when opaque #29236

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

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Dec 5, 2024

PR summary

In some cases, such as GIF output, Pillow must convert to P (palette) mode. Unfortunately, when done on RGBA images, this can lose some colour information (cf, python-pillow/Pillow#6832) But when the image starts in RGB mode, the conversion to P mode works better, so convert frames to RGB when they don't have any transparent pixels.

Fixes #29190

I'm not sure we have any tests for GIF, etc., other than smoke tests.

PR checklist

In some cases, such as GIF output, Pillow must convert to P (palette)
mode. Unfortunately, when done on RGBA images, this can lose some colour
information (cf, python-pillow/Pillow#6832)
But when the image starts in RGB mode, the conversion to P mode works
better, so convert frames to RGB when they don't have any transparent
pixels.

Fixes matplotlib#29190
@QuLogic QuLogic added this to the v3.10.0 milestone Dec 5, 2024
@NGWi
Copy link
Contributor

NGWi commented Dec 5, 2024

Awesome! I ran my bug reproduction script for issue #29190 against your branch and the saved animation finally looks the same as the plt.show() version.

@NGWi
Copy link
Contributor

NGWi commented Dec 5, 2024

I ran a pixel-to-pixel comparison script, and I'm impressed.
The only diffs I got were from the direct plot overlapping the gray part of the right border, while in the saved GIF it doesn't. I am attaching, below, the biggest diff pixel-wise (2 2 12), from frame 54. This overlapping could be a result of @QuLogic's changed transparency handling. However, I don't think it's something users will care about, plus the fact that the border overlaps into the graph is an issue unrelated to his changes.
I also got diffs on the axes' numbers, which I masked out. But I don't think they are noticeable either.
The main question is how to write a test when there are these trivial pixel differences.
frame_54_diff

@QuLogic
Copy link
Member Author

QuLogic commented Dec 5, 2024

The only diffs I got were from the direct plot overlapping the gray part of the right border, while in the saved GIF it doesn't. I am attaching, below, the biggest diff pixel-wise (2 2 12), from frame 54. This overlapping could be a result of @QuLogic's changed transparency handling. However, I don't think it's something users will care about, plus the fact that the border overlaps into the graph is an issue unrelated to his changes.

This is likely an inherent limitation of GIF due to the 256-colour palette. If you histogram the unique colours, you might find that there are 256 combinations already. Otherwise, it's sort of up to the quantizing in Pillow.

@timhoffm timhoffm merged commit 9d1a393 into matplotlib:main Dec 5, 2024
42 checks passed
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Dec 5, 2024
@QuLogic QuLogic deleted the anim-rgb branch December 5, 2024 23:06
timhoffm added a commit that referenced this pull request Dec 6, 2024
…236-on-v3.10.x

Backport PR #29236 on branch v3.10.x (ANI: Reduce Pillow frames to RGB when opaque)
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.

[Bug]: inconsistent ‘animation.FuncAnimation’ between display and save
4 participants