8000 Colormap alpha by efiring · Pull Request #660 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Colormap alpha #660

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 7 commits into from
Jan 8, 2012
Merged
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
Premultiply color-mapped images, since that is what the backends expect.
  • Loading branch information
mdboom committed Jan 5, 2012
commit a44ed2f487e071e554ea766ca76706531769e4b7
2 changes: 2 additions & 0 deletions lib/matplotlib/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ def _get_unsampled_image(self, A, image_extents, viewlim):
else:
if self._rgbacache is None:
x = self.to_rgba(self._A, bytes=True)
# premultiply the colors
x[...,0:3] *= x[...,3:4] / 255.0
Copy link
Contributor

Choose a reason for hiding this comment

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

This line gave me an error, actually:

TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'  

I had to change it to

x[...,0:3] = np.uint8(x[...,0:3] * (x[...,3:4] / 255.0))

But I'm not sure if x is guaranteed to be uint8.

Copy link
Member Author

Choose a reason for hiding this comment

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

With "bytes=True" in the line above, it is guaranteed to be uint8.
It looks like what we might want to do here, though, is leave "bytes=False", premultiply, and then convert to uint8. Or that could all be done inside to_rgba, controlled by another kwarg. (to_rgba is already a little bit hard to follow, though.)
What numpy version are you using? Mike's version worked on mine (1.6.2.dev-396dbb9).

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm running github master ('2.0.0.dev-b817938').

I'm not quite sure what you have in mind. The following wouldn't work

x = self.to_rgba(self._A)
x[...,0:3] *= x[...,3:4] / 255.0
x = np.uint8(x)

because x is not guaranteed to be float (right?). So would the alternative be something like

x = self.to_rgba(self._A)
rgb = x[...,0:3] * (x[...,3:4] / 255.0)
x = np.uint8(np.hstack(rgb, x[...,3]))

Copy link
Contributor

Choose a reason for hiding this comment

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

I see in your latest commits, you make sure that to_rgba returns normalized float values. Just confirm, this now works perfectly on my system.

self._rgbacache = x
else:
x = self._rgbacache
Expand Down
0