A community-maintained Python framework for creating mathematical animations.
Support for specifying the interpolation algorithms for individual ImageMobjects (#1122)
Adam Ryczkowski3af4c867d179
diff --git a/manim/camera/camera.py b/manim/camera/camera.pyindex f384cbf96..d87bdb15f 100644--- a/manim/camera/camera.py+++ b/manim/camera/camera.py@@ -890,7 +890,7 @@ class Camera:for image_mobject in image_mobjects:self.display_image_mobject(image_mobject, pixel_array)- def display_image_mobject(self, image_mobject, pixel_array):+ def display_image_mobject(self, image_mobject: AbstractImageMobject, pixel_array):"""Displays an ImageMobject by changing the pixel_array suitably.Parameters@@ -912,7 +912,7 @@ class Camera:pixel_width = max(int(pdist([ul_coords, ur_coords])), 1)pixel_height = max(int(pdist([ul_coords, dl_coords])), 1)sub_image = sub_image.resize(- (pixel_width, pixel_height), resample=Image.BICUBIC+ (pixel_width, pixel_height), resample=image_mobject.resampling_algorithm)# Rotate@@ -920,7 +920,7 @@ class Camera:adjusted_angle = -int(360 * angle / TAU)if adjusted_angle != 0:sub_image = sub_image.rotate(- adjusted_angle, resample=Image.BICUBIC, expand=1+ adjusted_angle, resample=image_mobject.resampling_algorithm, expand=1)# TODO, there is no accounting for a shear...diff --git a/manim/constants.py b/manim/constants.pyindex c0b00d164..18c5aee29 100644--- a/manim/constants.py+++ b/manim/constants.py@@ -5,6 +5,7 @@ Constant definitions.import typingimport numpy as np+from PIL import Image# MessagesNOT_SETTING_FONT_MSG: str = """@@ -42,6 +43,18 @@ ULTRABOLD: str = "ULTRABOLD"HEAVY: str = "HEAVY"ULTRAHEAVY: str = "ULTRAHEAVY"+RESAMPLING_ALGORITHMS = {+ "nearest": Image.NEAREST,+ "none": Image.NEAREST,+ "lanczos": Image.LANCZOS,+ "antialias": Image.LANCZOS,+ "bilinear": Image.BILINEAR,+ "linear": Image.BILINEAR,+ "bicubic": Image.BICUBIC,+ "cubic": Image.BICUBIC,+ "box": Image.BOX,+ "hamming": Image.HAMMING,+}# Geometry: directionsORIGIN: np.ndarray = np.array((0.0, 0.0, 0.0))diff --git a/manim/mobject/types/image_mobject.py b/manim/mobject/types/image_mobject.pyindex b14a6ce2e..d60437085 100644--- a/manim/mobject/types/image_mobject.py+++ b/manim/mobject/types/image_mobject.py@@ -30,9 +30,16 @@ class AbstractImageMobject(Mobject):This is a custom parameter of ImageMobject so that rendering a scene with e.g. the ``--quality low`` or ``--quality medium`` flag for faster rendering won't effect the position of the image on the screen."""- def __init__(self, scale_to_resolution, pixel_array_dtype="uint8", **kwargs):+ def __init__(+ self,+ scale_to_resolution,+ pixel_array_dtype="uint8",+ resampling_algorithm=Image.BICUBIC,+ **kwargs,+ ):self.pixel_array_dtype = pixel_array_dtypeself.scale_to_resolution = scale_to_resolution+ self.set_resampling_algorithm(resampling_algorithm)Mobject.__init__(self, **kwargs)def get_pixel_array(self):@@ -42,6 +49,28 @@ class AbstractImageMobject(Mobject):# Likely to be implemented in subclasses, but no obligationpass+ def set_resampling_algorithm(self, resampling_algorithm):+ """+ Sets the interpolation method for upscaling the image. By default the image is interpolated using bicubic algorithm. This method lets you change it.+ Interpolation is done internally using Pillow, and the function besides the string constants describing the algorithm accepts the Pillow integer constants.++ Parameters+ ----------+ resampling_algorithm : :class:`int`, an integer constant described in the Pillow library, or one from the RESAMPLING_ALGORITHMS global dictionary, under the following keys:+ * 'bicubic' or 'cubic'+ * 'nearest' or 'none'+ * 'box'+ * 'bilinear' or 'linear'+ * 'hamming'+ * 'lanczos' or 'antialias'+ """+ if isinstance(resampling_algorithm, int):+ self.resampling_algorithm = resampling_algorithm+ else:+ raise ValueError(+ "resampling_algorithm has to be an int, one of the values defined in RESAMPLING_ALGORITHMS or a Pillow resampling filter constant. Available algorithms: 'bicubic', 'nearest', 'box', 'bilinear', 'hamming', 'lanczos'."+ )+def reset_points(self):# Corresponding corners of image are fixed to these 3 pointsself.points = np.array(@@ -83,6 +112,41 @@ class ImageMobject(AbstractImageMobject):image.height = 7self.add(image)++ Changing interpolation style:++ .. manim:: ImageInterpolationEx+ :save_last_frame:++ class ImageInterpolationEx(Scene):+ def construct(self):+ img = ImageMobject(np.uint8([[63, 0, 0, 0],+ [0, 127, 0, 0],+ [0, 0, 191, 0],+ [0, 0, 0, 255]+ ]))++ img.height = 2+ img1 = img.copy()+ img2 = img.copy()+ img3 = img.copy()+ img4 = img.copy()+ img5 = img.copy()++ img1.set_resampling_algorithm(RESAMPLING_ALGORITHMS["nearest"])+ img2.set_resampling_algorithm(RESAMPLING_ALGORITHMS["lanczos"])+ img3.set_resampling_algorithm(RESAMPLING_ALGORITHMS["linear"])+ img4.set_resampling_algorithm(RESAMPLING_ALGORITHMS["cubic"])+ img5.set_resampling_algorithm(RESAMPLING_ALGORITHMS["box"])+ img1.add(Text("nearest").scale(0.5).next_to(img1,UP))+ img2.add(Text("lanczos").scale(0.5).next_to(img2,UP))+ img3.add(Text("linear").scale(0.5).next_to(img3,UP))+ img4.add(Text("cubic").scale(0.5).next_to(img4,UP))+ img5.add(Text("box").scale(0.5).next_to(img5,UP))++ x= Group(img1,img2,img3,img4,img5)+ x.arrange()+ self.add(x)"""def __init__(diff --git a/tests/control_data/graphical_units_data/img_and_svg/ImageInterpolationTest.npz b/tests/control_data/graphical_units_data/img_and_svg/ImageInterpolationTest.npznew file mode 100644index 000000000..2824ec627Binary files /dev/null and b/tests/control_data/graphical_units_data/img_and_svg/ImageInterpolationTest.npz differdiff --git a/tests/test_graphical_units/test_img_and_svg.py b/tests/test_graphical_units/test_img_and_svg.pyindex 2dd0be372..2e3775722 100644--- a/tests/test_graphical_units/test_img_and_svg.py+++ b/tests/test_graphical_units/test_img_and_svg.py@@ -232,6 +232,30 @@ class ImageMobjectTest(Scene):self.wait(1)+class ImageInterpolationTest(Scene):+ def construct(self):+ img = ImageMobject(+ np.uint8([[63, 0, 0, 0], [0, 127, 0, 0], [0, 0, 191, 0], [0, 0, 0, 255]])+ )++ img.height = 2+ img1 = img.copy()+ img2 = img.copy()+ img3 = img.copy()+ img4 = img.copy()+ img5 = img.copy()++ img1.set_resampling_algorithm(RESAMPLING_ALGORITHMS["nearest"])+ img2.set_resampling_algorithm(RESAMPLING_ALGORITHMS["lanczos"])+ img3.set_resampling_algorithm(RESAMPLING_ALGORITHMS["linear"])+ img4.set_resampling_algorithm(RESAMPLING_ALGORITHMS["cubic"])+ img5.set_resampling_algorithm(RESAMPLING_ALGORITHMS["box"])++ self.add(img1, img2, img3, img4, img5)+ [s.shift(4 * LEFT + pos * 2 * RIGHT) for pos, s in enumerate(self.mobjects)]+ self.wait()++MODULE_NAME = "img_and_svg"