Skip to content

Commit acdefe6

Browse files
committed
Remove lanczos, box and hamming resampling algorithms
1 parent 23ec1f3 commit acdefe6

File tree

6 files changed

+32
-37
lines changed

6 files changed

+32
-37
lines changed

manim/camera/camera.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -962,15 +962,15 @@ def display_multiple_image_mobjects(
962962

963963
def display_image_mobject(
964964
self, image_mobject: AbstractImageMobject, pixel_array: np.ndarray
965-
):
966-
"""Displays an ImageMobject by changing the pixel_array suitably.
965+
) -> None:
966+
"""Displays an :class:`~.ImageMobject` by changing the ``pixel_array`` suitably.
967967
968968
Parameters
969969
----------
970970
image_mobject
971-
The imageMobject to display
971+
The :class:`~.ImageMobject` to display.
972972
pixel_array
973-
The Pixel array to put the imagemobject in.
973+
The pixel array to put the :class:`~.ImageMobject` in.
974974
"""
975975
sub_image = Image.fromarray(image_mobject.get_pixel_array(), mode="RGBA")
976976
original_coords = np.array(
@@ -982,6 +982,9 @@ def display_image_mobject(
982982
]
983983
)
984984
target_coords = self.points_to_pixel_coords(image_mobject, image_mobject.points)
985+
986+
# Temporarily translate target coords to upper left corner to calculate the
987+
# smallest possible size for the target image.
985988
shift_vector = np.array(
986989
[
987990
min(*[x for x, y in target_coords]),
@@ -994,27 +997,29 @@ def display_image_mobject(
994997
max(*[y for x, y in target_coords]),
995998
)
996999

1000+
# Use PIL.Image.Image.transform() to apply a perspective transform to the image.
1001+
# The transform coefficients must be calculated. The following is adapted from
1002+
# https://stackoverflow.com/questions/14177744/how-does-perspective-transformation-work-in-pil
1003+
# and
1004+
# https://web.archive.org/web/20150222120106/xenia.media.mit.edu/~cwren/interpolator/
1005+
9971006
homographic_matrix = []
998-
for tc, oc in zip(target_coords, original_coords):
999-
homographic_matrix.append(
1000-
[tc[0], tc[1], 1, 0, 0, 0, -oc[0] * tc[0], -oc[0] * tc[1]]
1001-
)
1002-
homographic_matrix.append(
1003-
[0, 0, 0, tc[0], tc[1], 1, -oc[1] * tc[0], -oc[1] * tc[1]]
1004-
)
1007+
for (x, y), (X, Y) in zip(target_coords, original_coords):
1008+
homographic_matrix.append([x, y, 1, 0, 0, 0, -X * x, -X * y])
1009+
homographic_matrix.append([0, 0, 0, x, y, 1, -Y * x, -Y * y])
10051010

10061011
A = np.array(homographic_matrix, dtype=ManimFloat)
10071012
b = original_coords.reshape(8).astype(ManimFloat)
10081013
transform_coefficients = np.linalg.solve(A, b)
10091014

10101015
sub_image = sub_image.transform(
1011-
size=target_size, # Use the smallest possible size for speed
1016+
size=target_size, # Use the smallest possible size for speed.
10121017
method=Image.Transform.PERSPECTIVE,
10131018
data=transform_coefficients,
10141019
resample=image_mobject.resampling_algorithm,
10151020
)
10161021

1017-
# Paste into an image as large as the camera's pixel array
1022+
# Paste into an image as large as the camera's pixel array.
10181023
full_image = Image.fromarray(
10191024
np.zeros((self.pixel_height, self.pixel_width)),
10201025
mode="RGBA",
@@ -1028,7 +1033,7 @@ def display_image_mobject(
10281033
shift_vector[1] + target_size[1],
10291034
),
10301035
)
1031-
# Paint on top of existing pixel array
1036+
# Paint on top of existing pixel array.
10321037
self.overlay_PIL_image(pixel_array, full_image)
10331038

10341039
def overlay_rgba_array(self, pixel_array: np.ndarray, new_array: np.ndarray):

manim/constants.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,10 @@
111111
RESAMPLING_ALGORITHMS = {
112112
"nearest": Resampling.NEAREST,
113113
"none": Resampling.NEAREST,
114-
"lanczos": Resampling.LANCZOS,
115-
"antialias": Resampling.LANCZOS,
116114
"bilinear": Resampling.BILINEAR,
117115
"linear": Resampling.BILINEAR,
118116
"bicubic": Resampling.BICUBIC,
119117
"cubic": Resampling.BICUBIC,
120-
"box": Resampling.BOX,
121-
"hamming": Resampling.HAMMING,
122118
}
123119

124120
# Geometry: directions

manim/mobject/types/image_mobject.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ def set_resampling_algorithm(self, resampling_algorithm: int) -> Self:
8585
* 'hamming'
8686
* 'lanczos' or 'antialias'
8787
"""
88-
if isinstance(resampling_algorithm, int):
89-
self.resampling_algorithm = resampling_algorithm
90-
else:
88+
if resampling_algorithm not in RESAMPLING_ALGORITHMS.values():
9189
raise ValueError(
9290
"resampling_algorithm has to be an int, one of the values defined in "
9391
"RESAMPLING_ALGORITHMS or a Pillow resampling filter constant. "
94-
"Available algorithms: 'bicubic', 'nearest', 'box', 'bilinear', "
95-
"'hamming', 'lanczos'.",
92+
"Available algorithms: 'bicubic' (or 'cubic'), 'nearest' (or 'none'), "
93+
"'bilinear' (or 'linear').",
9694
)
95+
96+
self.resampling_algorithm = resampling_algorithm
9797
return self
9898

9999
def reset_points(self) -> None:
Binary file not shown.
Binary file not shown.

tests/test_graphical_units/test_img_and_svg.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,20 +269,14 @@ def test_ImageInterpolation(scene):
269269
np.uint8([[63, 0, 0, 0], [0, 127, 0, 0], [0, 0, 191, 0], [0, 0, 0, 255]]),
270270
)
271271
img.height = 2
272-
img1 = img.copy()
273-
img2 = img.copy()
274-
img3 = img.copy()
275-
img4 = img.copy()
276-
img5 = img.copy()
277-
278-
img1.set_resampling_algorithm(RESAMPLING_ALGORITHMS["nearest"])
279-
img2.set_resampling_algorithm(RESAMPLING_ALGORITHMS["lanczos"])
280-
img3.set_resampling_algorithm(RESAMPLING_ALGORITHMS["linear"])
281-
img4.set_resampling_algorithm(RESAMPLING_ALGORITHMS["cubic"])
282-
img5.set_resampling_algorithm(RESAMPLING_ALGORITHMS["box"])
283-
284-
scene.add(img1, img2, img3, img4, img5)
285-
[s.shift(4 * LEFT + pos * 2 * RIGHT) for pos, s in enumerate(scene.mobjects)]
272+
273+
algorithms = ["nearest", "bilinear", "bicubic"]
274+
for pos, algorithm in enumerate(algorithms):
275+
algorithm = RESAMPLING_ALGORITHMS[algorithm]
276+
img_copy = img.copy().set_resampling_algorithm(algorithm)
277+
img_copy.shift((2 * pos - len(algorithms) + 1) * RIGHT)
278+
scene.add(img_copy)
279+
286280
scene.wait()
287281

288282

0 commit comments

Comments
 (0)