-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Conversation
See the thread "strange behavior of images when they have an alpha channel" on matplotlib-users. |
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. |
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. |
Patch verified to work under Windows as well. |
Also testing saving a PNG with the sample script, works as well under both Windows and Linux. |
…the backends. Tested and works on Agg, Pdf and Svg. (Ps does not support alpha).
The patch to _image.cpp (discussed above) also verified to work under OSX. |
Broken alpha handling in images
As reported by Daniel Hyams, matplotlib does not handle alpha blending in images correctly.
Agg expects premultiplied alpha, but we often send it unmultiplied alpha
set_alpha on the Image artist has no effect