Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Broken alpha handling in images #545

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 2 commits into from
Oct 28, 2011

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented Oct 24, 2011

As reported by Daniel Hyams, matplotlib does not handle alpha blending in images correctly.

  1. Agg expects premultiplied alpha, but we often send it unmultiplied alpha

  2. set_alpha on the Image artist has no effect

@ghost ghost assigned mdboom Oct 21, 2011
@mdboom
Copy link
Member Author

mdboom commented Oct 21, 2011

See the thread "strange behavior of images when they have an alpha channel" on matplotlib-users.

@dhyams
Copy link
Contributor

dhyams commented Oct 22, 2011

The following patch supplied by mdboom indeed works! I don't understand how yet; maybe Michael can explain ;)

diff --git a/src/_image.cpp b/src/_image.cpp
index 3278b6c..6bb202a 100644
--- a/src/_image.cpp
+++ b/src/_image.cpp
@@ -33,7 +33,7 @@
 #include "mplutils.h"


-typedef agg::pixfmt_rgba32 pixfmt;
+typedef agg::pixfmt_rgba32_pre pixfmt;
 typedef agg::renderer_base<pixfmt> renderer_base;
 typedef agg::span_interpolator_linear<> interpolator_type;
 typedef agg::rasterizer_scanline_aa<agg::rasterizer_sl_clip_dbl> rasterizer;

Just to supply more detail about the testing procedure, just in case in matters...I applied the patch, rebuilt matplotlib, and replaced only libs _image.so and _backend_agg.so in my installed matplotlib distribution.

Sample script used to test:

import matplotlib
import numpy
from matplotlib import pyplot as plt
import matplotlib.image
import matplotlib.transforms
import Image # PIL required.

fig = plt.figure()
ax = fig.gca()
ax.grid(True)
fig.patch.set_facecolor('white')

# read in and store the image
im_orig = Image.open("star2.png")

# create a second image with the entire alpha channel set to 127.
pix = numpy.array(im_orig)
#pix[:,:,3] = 127
pix *= 0.5
im_alpha = Image.fromarray(pix)


# create mpl image 1; just the raw image.
# bilinear interpolation.
ax.text(0.25,0.75,"Original Image, bilinear interp",ha='center',va='center',zorder=200)
xy1 = ax.transAxes.transform([0.0,0.5])
xy2 = ax.transAxes.transform([0.5,1.0])
bbox = matplotlib.transforms.Bbox([xy1,xy2])
bboxim = matplotlib.image.BboxImage(bbox,origin='lower',interpolation='bilinear',zorder=100)
bboxim.set_data(im_orig)
ax.artists.append(bboxim)

# create mpl image 2: the image with the entire alpha channel set to 200.
# bilinear interpolation.
ax.text(0.75,0.75,"alpha=127 Image, bilinear interp",ha='center',va='center',zorder=200)
xy1 = ax.transAxes.transform([0.5,0.5])
xy2 = ax.transAxes.transform([1.0,1.0])
bbox = matplotlib.transforms.Bbox([xy1,xy2])
bboxim = matplotlib.image.BboxImage(bbox,origin='lower',interpolation='bilinear',zorder=100)
bboxim.set_data(im_alpha)
ax.artists.append(bboxim)


# create mpl image 3: the image with the entire alpha channel set to 200.
# "nearest" interpolation.
ax.text(0.75,0.25,"alpha=127 Image, nearest interp",ha='center',va='center',zorder=200)
xy1 = ax.transAxes.transform([0.5,0.0])
xy2 = ax.transAxes.transform([1.0,0.5])
bbox = matplotlib.transforms.Bbox([xy1,xy2])
bboxim = matplotlib.image.BboxImage(bbox,origin='lower',interpolation='nearest',zorder=100)
bboxim.set_data(im_alpha)
ax.artists.append(bboxim)

This still needs to be tested with multiple backends and multiple platforms, but based on what I've seen so far, the multiplatform part of it should not matter...can't say anything about the other backends (mine is wxagg). But the fact that it worked straightaway was encouraging.

@dhyams
Copy link
Contributor

dhyams commented Oct 22, 2011

After I wrote the above comment, I realized that there was no need to replace _backend_agg.so. Therefore, I did a quick test with the original _backend_agg.so with the mdboom patched _image.so, and it still worked fine as expected.

@dhyams
Copy link
Contributor

dhyams commented Oct 22, 2011

Patch verified to work under Windows as well.

@dhyams
Copy link
Contributor

dhyams commented Oct 23, 2011

Also testing saving a PNG with the sample script, works as well under both Windows and Linux.

@dhyams
Copy link
Contributor

dhyams commented Oct 24, 2011

The patch to _image.cpp (discussed above) also verified to work under OSX.

mdboom added a commit that referenced this pull request Oct 28, 2011
@mdboom mdboom merged commit 0ec7c7e into matplotlib:v1.1.x Oct 28, 2011
@mdboom mdboom deleted the alpha_image_resize_bug branch March 3, 2015 18:43
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.

2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.