Skip to content

Properly handle transparency for animations #5415

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

Closed
wants to merge 8 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Appease the PEP8 gods. Somewhat of a sacraficial lamb if you ask me.
  • Loading branch information
cbreeden committed Nov 25, 2015
commit a57be1ff208c7828c5351a49c1b2ca26f2bc0967
17 changes: 9 additions & 8 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ def thumbnail(infile, thumbfile, scale=0.1, interpolation='bilinear',
fig.savefig(thumbfile, dpi=dpi)
return fig


def flatten_rgba(src, bg=None):
"""
Flatten an RGBA image *src* with a background color *bg*.
Expand All @@ -1517,7 +1518,7 @@ def flatten_rgba(src, bg=None):

bg : Tuple(int,int,int), optional
Background color to merge *src* with. If no bg color is provided
the color from the rcParam 'savefig.facecolor' will be use.
the color from the rcParam 'savefig.facecolor' will be used.

Returns
-------
Expand All @@ -1529,18 +1530,18 @@ def flatten_rgba(src, bg=None):
rcParams.get('savefig.facecolor', 'white'))
bg = tuple([int(x * 255.0) for x in bg])

# Numpy images have dtype=uint8 which will overflow
# Numpy images have dtype=uint8 which will overflow for these calculations
src = src.astype(np.uint16)

alpha = src[:,:,3]
src_rgb = src[:,:,:3]
alpha = src[:, :, 3]
src_rgb = src[:, :, :3]
w, h, _ = src.shape

dest = np.empty((w, h, 4))
dest[:,:,0] = (255 - alpha)*bg[0] + alpha*src_rgb[:,:,0]
dest[:,:,1] = (255 - alpha)*bg[1] + alpha*src_rgb[:,:,1]
dest[:,:,2] = (255 - alpha)*bg[2] + alpha*src_rgb[:,:,2]
dest[:, :, 0] = (255 - alpha)*bg[0] + alpha*src_rgb[:, :, 0]
dest[:, :, 1] = (255 - alpha)*bg[1] + alpha*src_rgb[:, :, 1]
dest[:, :, 2] = (255 - alpha)*bg[2] + alpha*src_rgb[:, :, 2]
dest = (dest/255).astype(np.uint8)
dest[:,:,3] = 255
dest[:, :, 3] = 255

return dest