Skip to content

Commit 2f10e68

Browse files
committed
Take Image object's alpha attribute into account when compositing multiple images together.
1 parent 668690d commit 2f10e68

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

lib/matplotlib/axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2054,7 +2054,7 @@ def draw(self, renderer=None, inframe=False):
20542054
zorder_images.sort(key=lambda x: x[0])
20552055

20562056
mag = renderer.get_image_magnification()
2057-
ims = [(im.make_image(mag), 0, 0) for z, im in zorder_images]
2057+
ims = [(im.make_image(mag), 0, 0, im.get_alpha()) for z, im in zorder_images]
20582058

20592059
l, b, r, t = self.bbox.extents
20602060
width = mag * ((round(r) + 0.5) - (round(l) - 0.5))

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def draw(self, renderer):
990990
# make a composite image blending alpha
991991
# list of (_image.Image, ox, oy)
992992
mag = renderer.get_image_magnification()
993-
ims = [(im.make_image(mag), im.ox, im.oy)
993+
ims = [(im.make_image(mag), im.ox, im.oy, im.get_alpha())
994994
for im in self.images]
995995

996996
im = _image.from_images(self.bbox.height * mag,

src/_image.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ _image_module::from_images(const Py::Tuple& args)
796796
Py::Tuple tup;
797797

798798
size_t ox(0), oy(0), thisx(0), thisy(0);
799+
float alpha;
800+
bool apply_alpha;
799801

800802
//copy image 0 output buffer into return images output buffer
801803
Image* imo = new Image;
@@ -823,6 +825,16 @@ _image_module::from_images(const Py::Tuple& args)
823825
Image* thisim = static_cast<Image*>(tup[0].ptr());
824826
ox = (long)Py::Int(tup[1]);
825827
oy = (long)Py::Int(tup[2]);
828+
if (tup[3].ptr() == Py_None)
829+
{
830+
apply_alpha = false;
831+
}
832+
else
833+
{
834+
apply_alpha = true;
835+
alpha = Py::Float(tup[3]);
836+
}
837+
826838
bool isflip = (thisim->rbufOut->stride()) < 0;
827839
//std::cout << "from images " << isflip << "; stride=" << thisim->rbufOut->stride() << std::endl;
828840
size_t ind = 0;
@@ -851,7 +863,14 @@ _image_module::from_images(const Py::Tuple& args)
851863
p.r = *(thisim->bufferOut + ind++);
852864
p.g = *(thisim->bufferOut + ind++);
853865
p.b = *(thisim->bufferOut + ind++);
854-
p.a = *(thisim->bufferOut + ind++);
866+
if (apply_alpha)
867+
{
868+
p.a = (pixfmt::value_type) *(thisim->bufferOut + ind++) * alpha;
869+
}
870+
else
871+
{
872+
p.a = *(thisim->bufferOut + ind++);
873+
}
855874
pixf.blend_pixel(thisx, thisy, p, 255);
856875
}
857876
}

0 commit comments

Comments
 (0)