Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mdboom merged 2 commits intomatplotlib:v1.1.xfrommdboom:alpha_image_resize_bug
Oct 28, 2011

Conversation

mdboom
Copy link
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

@ghostghost assignedmdboomOct 21, 2011
@mdboom
Copy link
MemberAuthor

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

@dhyams
Copy link
Contributor

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

diff--gita/src/_image.cppb/src/_image.cppindex3278b6c..6bb202a100644---a/src/_image.cpp+++b/src/_image.cpp@@-33,7+33,7 @@#include"mplutils.h"-typedefagg::pixfmt_rgba32pixfmt;+typedefagg::pixfmt_rgba32_prepixfmt;typedefagg::renderer_base<pixfmt>renderer_base;typedefagg::span_interpolator_linear<>interpolator_type;typedefagg::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:

importmatplotlibimportnumpyfrommatplotlibimportpyplotaspltimportmatplotlib.imageimportmatplotlib.transformsimportImage# PIL required.fig=plt.figure()ax=fig.gca()ax.grid(True)fig.patch.set_facecolor('white')# read in and store the imageim_orig=Image.open("star2.png")# create a second image with the entire alpha channel set to 127.pix=numpy.array(im_orig)#pix[:,:,3] = 127pix*=0.5im_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

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

Patch verified to work under Windows as well.

@dhyams
Copy link
Contributor

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

@dhyams
Copy link
Contributor

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

mdboom added a commit that referenced this pull requestOct 28, 2011
@mdboommdboom merged commit0ec7c7e intomatplotlib:v1.1.xOct 28, 2011
@mdboommdboom deleted the alpha_image_resize_bug branchMarch 3, 2015 18:43
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees

@mdboommdboom

Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants
@mdboom@dhyams

[8]ページ先頭

©2009-2025 Movatter.jp