Broken alpha handling in images #545

merged 2 commits into from Oct 28, 2011

2 participants

Matplotlib Developers member

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

@mdboom mdboom was assigned Oct 21, 2011
Matplotlib Developers member

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 and 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()

# read in and store the image
im_orig ="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)

# 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)

# 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)

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 Therefore, I did a quick test with the original with the mdboom patched, 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 patch to _image.cpp (discussed above) also verified to work under OSX.

@mdboom mdboom merged commit 0ec7c7e into matplotlib:v1.1.x Oct 28, 2011
@mdboom mdboom deleted the mdboom:alpha_image_resize_bug branch Mar 3, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment