Skip to content

Commit e64ce7f

Browse files
committed
Fixed alpha operations on (PNG) images with transparency
Previously, calling set_opacity(alpha) would have replaced the original alpha channel, thus making previously invisible parts visible.
1 parent aeeb6da commit e64ce7f

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

manim/mobject/types/image_mobject.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ def __init__(
189189
)
190190
if self.invert:
191191
self.pixel_array[:, :, :3] = 255 - self.pixel_array[:, :, :3]
192+
self.orig_alpha_pixel_array = self.pixel_array[:, :, 3].copy()
192193
super().__init__(scale_to_resolution, **kwargs)
193194

194195
def get_pixel_array(self):
@@ -199,7 +200,7 @@ def set_color(self, color, alpha=None, family=True):
199200
rgb = color_to_int_rgb(color)
200201
self.pixel_array[:, :, :3] = rgb
201202
if alpha is not None:
202-
self.pixel_array[:, :, 3] = int(255 * alpha)
203+
self.pixel_array[:, :, 3] = int(self.orig_alpha_pixel_array * alpha)
203204
for submob in self.submobjects:
204205
submob.set_color(color, alpha, family)
205206
self.color = color
@@ -214,7 +215,7 @@ def set_opacity(self, alpha):
214215
The alpha value of the object, 1 being opaque and 0 being
215216
transparent.
216217
"""
217-
self.pixel_array[:, :, 3] = int(255 * alpha)
218+
self.pixel_array[:, :, 3] = (self.orig_alpha_pixel_array * alpha).astype(int)
218219
self.fill_opacity = alpha
219220
self.stroke_opacity = alpha
220221
return self

0 commit comments

Comments
 (0)