Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Auto resize Images from ImageMobject (#605)

kolibril13733405dcc34e
diff --git a/manim/mobject/types/image_mobject.py b/manim/mobject/types/image_mobject.py
index 1158872c5..6180dbd68 100644
--- a/manim/mobject/types/image_mobject.py
+++ b/manim/mobject/types/image_mobject.py
@@ -2,11 +2,13 @@
__all__ = ["AbstractImageMobject", "ImageMobject", "ImageMobjectFromCamera"]
+import pathlib
import numpy as np
from PIL import Image
+from ... import config
from ...constants import *
from ...mobject.mobject import Mobject
from ...mobject.shape_matchers import SurroundingRectangle
@@ -14,18 +16,31 @@ from ...utils.bezier import interpolate
from ...utils.color import color_to_int_rgb, WHITE
from ...utils.config_ops import digest_config
from ...utils.images import get_full_raster_image_path
+from manim.constants import QUALITIES, DEFAULT_QUALITY
class AbstractImageMobject(Mobject):
"""
Automatically filters out black pixels
+
+ Parameters
+ ----------
+ scale_to_resolution : :class:`int`
+ At this resolution the image is placed pixel by pixel onto the screen, so it will look the sharpest and best.
+ This is a custom parameter of ImageMobject so that rendering a scene with the `--quality low` flag for faster rendering and testing won't effect the position of the image on the screen.
+ Calculated by `height = image_height / scale_to_resolution * config["frame_height"]`
"""
CONFIG = {
- "height": 2.0,
"pixel_array_dtype": "uint8",
}
+ def __init__(self, scale_to_resolution, **kwargs):
+ digest_config(self, kwargs)
+ self.scale_to_resolution = scale_to_resolution
+
+ Mobject.__init__(self, **kwargs)
+
def get_pixel_array(self):
raise NotImplementedError()
@@ -44,6 +59,10 @@ class AbstractImageMobject(Mobject):
)
self.center()
h, w = self.get_pixel_array().shape[:2]
+ if self.scale_to_resolution:
+ self.height = h / self.scale_to_resolution * config["frame_height"]
+ else:
+ self.height = 3 ## this is the case for ImageMobjectFromCamera
self.stretch_to_fit_height(self.height)
self.stretch_to_fit_width(self.height * w / h)
@@ -51,6 +70,15 @@ class AbstractImageMobject(Mobject):
class ImageMobject(AbstractImageMobject):
"""Displays an Image from a numpy array or a file.
+ Parameters
+ ----------
+ scale_to_resolution : :class:`int`
+ At this resolution the image is placed pixel by pixel onto the screen, so it will look the sharpest and best.
+ This is a custom parameter of ImageMobject so that rendering a scene with the `--quality low` flag for faster rendering and testing won't effect the position of the image on the screen.
+ Calculated by `height = image_height / scale_to_resolution * config["frame_height"]`
+
+
+
Example
-------
.. manim:: ImageFromArray
@@ -70,9 +98,14 @@ class ImageMobject(AbstractImageMobject):
"image_mode": "RGBA",
}
- def __init__(self, filename_or_array, **kwargs):
+ def __init__(
+ self,
+ filename_or_array,
+ scale_to_resolution=QUALITIES[DEFAULT_QUALITY]["pixel_height"],
+ **kwargs,
+ ):
digest_config(self, kwargs)
- if isinstance(filename_or_array, str):
+ if isinstance(filename_or_array, (str, pathlib.PurePath)):
path = get_full_raster_image_path(filename_or_array)
image = Image.open(path).convert(self.image_mode)
self.pixel_array = np.array(image)
@@ -81,7 +114,7 @@ class ImageMobject(AbstractImageMobject):
self.change_to_rgba_array()
if self.invert:
self.pixel_array[:, :, :3] = 255 - self.pixel_array[:, :, :3]
- AbstractImageMobject.__init__(self, **kwargs)
+ AbstractImageMobject.__init__(self, scale_to_resolution, **kwargs)
def change_to_rgba_array(self):
pa = self.pixel_array
@@ -146,7 +179,7 @@ class ImageMobjectFromCamera(AbstractImageMobject):
def __init__(self, camera, **kwargs):
self.camera = camera
self.pixel_array = self.camera.pixel_array
- AbstractImageMobject.__init__(self, **kwargs)
+ AbstractImageMobject.__init__(self, scale_to_resolution=False, **kwargs)
# TODO: Get rid of this.
def get_pixel_array(self):
diff --git a/manim/utils/file_ops.py b/manim/utils/file_ops.py
index e5b7c0320..2296a6bf0 100644
--- a/manim/utils/file_ops.py
+++ b/manim/utils/file_ops.py
@@ -32,14 +32,13 @@ def guarantee_existence(path):
def seek_full_path_from_defaults(file_name, default_dir, extensions):
possible_paths = [file_name]
possible_paths += [
- os.path.join(default_dir, file_name + extension)
- for extension in ["", *extensions]
+ Path(default_dir) / f"{file_name}{extension}" for extension in ["", *extensions]
]
for path in possible_paths:
if os.path.exists(path):
return path
error = "From: {}, could not find {} at either of these locations: {}".format(
- os.getcwd(), self.file_name, possible_paths
+ os.getcwd(), file_name, possible_paths
)
raise IOError(error)