From 823640ab0c22a74209b26132b4ed3f7867ba9672 Mon Sep 17 00:00:00 2001 From: gokuld Date: Thu, 15 Apr 2021 10:39:30 +0000 Subject: [PATCH] Bug fix: for rotating mask images. (#1399) * Bug fix: for rotating mask images. * Formatted code with black --- moviepy/video/fx/rotate.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/moviepy/video/fx/rotate.py b/moviepy/video/fx/rotate.py index 1f88f4ed3..2ad41502c 100644 --- a/moviepy/video/fx/rotate.py +++ b/moviepy/video/fx/rotate.py @@ -143,11 +143,23 @@ def filter(get_frame, t): UserWarning, ) + # PIL expects uint8 type data. However a mask image has values in the + # range [0, 1] and is of float type. To handle this we scale it up by + # a factor 'a' for use with PIL and then back again by 'a' afterwards. + if im.dtype == "float64": + # this is a mask image + a = 255.0 + else: + a = 1 + # call PIL.rotate - return np.array( - Image.fromarray(np.array(im).astype(np.uint8)).rotate( - angle, expand=expand, resample=resample, **kwargs + return ( + np.array( + Image.fromarray(np.array(a * im).astype(np.uint8)).rotate( + angle, expand=expand, resample=resample, **kwargs + ) ) + / a ) return clip.transform(filter, apply_to=["mask"])