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
Fix width/height locations, and forgot to add alpha layer to flatten_…
…rgba
  • Loading branch information
cbreeden committed Nov 25, 2015
commit b8175a144dbe4bcde6aa1ac05f9e56ba59b61bef
9 changes: 5 additions & 4 deletions lib/matplotlib/backends/backend_agg.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,13 +542,14 @@ def flatten_rgba(self, src, bg=None):

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

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

def print_png(self, filename_or_obj, *args, **kwargs):
Expand All @@ -566,8 +567,8 @@ def print_png(self, filename_or_obj, *args, **kwargs):
if kwargs.get('flatten', False):
w, h = int(renderer.width), int(renderer.height)
img = renderer._renderer.buffer_rgba()
img = np.array(memoryview(img))
img = self.flatten_rgba(img).reshape((h, w, 4))
img = np.array(memoryview(img)).reshape((h,w,4))
img = self.flatten_rgba(img)
_png.write_png(img, filename_or_obj, self.figure.dpi)
else:
_png.write_png(renderer._renderer, filename_or_obj, self.figure.dpi)
Copy link
Member

Choose a reason for hiding this comment

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

Minor point, but I think I like the way you organized the code in print_raw better than here: i.e. set a local variable img to the render contents and then optionally flatten it, ultimately passing img into write_png on a single line.

Copy link
Author

Choose a reason for hiding this comment

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

@mdboom I really wanted to do that at first, but I will be honest when I say that I didn't understand what was going on here:

RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi)
. I wasn't exactly sure if write_png needed something from that or not.

Copy link
Member

Choose a reason for hiding this comment

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

No -- it shouldn't matter. _write_png just converts whatever is passed in to a numpy array.

Expand Down