Skip to content

Commit 3e9f4a6

Browse files
committed
Merge pull request #1955 from Westacular/alpha-image-composite
Honouring the alpha attribute when creating composite images.
2 parents 3ed1397 + baeb6de commit 3e9f4a6

File tree

8 files changed

+263
-3
lines changed

8 files changed

+263
-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/backends/backend_cairo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ def draw_image(self, gc, x, y, im):
163163
buf, cairo.FORMAT_ARGB32, cols, rows, cols*4)
164164
ctx = gc.ctx
165165
y = self.height - y - rows
166+
167+
ctx.save()
166168
ctx.set_source_surface (surface, x, y)
167169
ctx.paint()
170+
ctx.restore()
168171

169172
im.flipud_out()
170173

lib/matplotlib/figure.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,7 @@ def draw(self, renderer):
997997
# make a composite image blending alpha
998998
# list of (_image.Image, ox, oy)
999999
mag = renderer.get_image_magnification()
1000-
ims = [(im.make_image(mag), im.ox, im.oy)
1000+
ims = [(im.make_image(mag), im.ox, im.oy, im.get_alpha())
10011001
for im in self.images]
10021002

10031003
im = _image.from_images(self.bbox.height * mag,
Binary file not shown.
55.8 KB
Loading

lib/matplotlib/tests/baseline_images/test_image/image_composite_alpha.svg

Lines changed: 212 additions & 0 deletions
Loading

lib/matplotlib/tests/test_image.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,32 @@ def test_image_composite_background():
238238
ax.set_axis_bgcolor((1, 0, 0, 0.5))
239239
ax.set_xlim([0, 12])
240240

241+
@image_comparison(baseline_images=['image_composite_alpha'], remove_text=True)
242+
def test_image_composite_alpha():
243+
"""
244+
Tests that the alpha value is recognized and correctly applied in the
245+
process of compositing images together.
246+
"""
247+
fig = plt.figure()
248+
ax = fig.add_subplot(111)
249+
arr = np.zeros((11, 21, 4))
250+
arr[:, :, 0] = 1
251+
arr[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))
252+
arr2 = np.zeros((21, 11, 4))
253+
arr2[:, :, 0] = 1
254+
arr2[:, :, 1] = 1
255+
arr2[:, :, 3] = np.concatenate((np.arange(0, 1.1, 0.1), np.arange(0, 1, 0.1)[::-1]))[:, np.newaxis]
256+
ax.imshow(arr, extent=[1, 2, 5, 0], alpha=0.3)
257+
ax.imshow(arr, extent=[2, 3, 5, 0], alpha=0.6)
258+
ax.imshow(arr, extent=[3, 4, 5, 0])
259+
ax.imshow(arr2, extent=[0, 5, 1, 2])
260+
ax.imshow(arr2, extent=[0, 5, 2, 3], alpha=0.6)
261+
ax.imshow(arr2, extent=[0, 5, 3, 4], alpha=0.3)
262+
ax.set_axis_bgcolor((0, 0.5, 0, 1))
263+
ax.set_xlim([0, 5])
264+
ax.set_ylim([5, 0])
265+
266+
241267
if __name__=='__main__':
242268
import nose
243269
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

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.size() <= 3 || 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)