Skip to content

Commit

Permalink
Tests all 'rotate' FX arguments combinations
Browse files Browse the repository at this point in the history
  • Loading branch information
mondeja committed Jan 23, 2021
1 parent 736abcc commit 487447b
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 22 deletions.
3 changes: 2 additions & 1 deletion moviepy/video/fx/rotate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import warnings

import numpy as np
Expand Down Expand Up @@ -99,7 +100,7 @@ def filter(get_frame, t):
im = get_frame(t)

if unit == "rad":
angle = 360.0 * angle / (2 * np.pi)
angle = math.degrees(angle)

angle %= 360
if not center and not translate and not bg_color:
Expand Down
130 changes: 109 additions & 21 deletions tests/test_fx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import os
import random

Expand Down Expand Up @@ -495,30 +496,117 @@ def test_resize():
# clip4.write_videofile(os.path.join(TMP_DIR, "resize4.webm"))


# Run several times to ensure that adding 360 to rotation angles has no effect
@pytest.mark.parametrize("angle_offset", [-360, 0, 360, 720])
def test_rotate(angle_offset):
# Run several times to ensure that adding 360 to rotation angles has no effect
clip = BitmapClip([["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]], fps=1)

clip1 = rotate(clip, 0 + angle_offset)
target1 = BitmapClip([["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]], fps=1)
assert clip1 == target1

clip2 = rotate(clip, 90 + angle_offset)
target2 = BitmapClip(
[["ABC", "ABC", "ABC", "ABC"], ["DEA", "CDE", "BCD", "ABC"]], fps=1
)
assert clip2 == target2, clip2.to_bitmap()
@pytest.mark.parametrize("unit", ["deg", "rad"])
@pytest.mark.parametrize("resample", ["bilinear", "nearest", "bicubic", "unknown"])
@pytest.mark.parametrize(
(
"angle",
"translate",
"center",
"bg_color",
"expected_frames",
),
(
(
0,
None,
None,
None,
[["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]],
),
(
90,
None,
None,
None,
[["ABC", "ABC", "ABC", "ABC"], ["DEA", "CDE", "BCD", "ABC"]],
),
(
180,
None,
None,
None,
[["CCCC", "BBBB", "AAAA"], ["AEDC", "EDCB", "DCBA"]],
),
(
270,
None,
None,
None,
[["CBA", "CBA", "CBA", "CBA"], ["CBA", "DCB", "EDC", "AED"]],
),
(
45,
(50, 50),
None,
(0, 255, 0),
[
["GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG"],
["GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG", "GGGGGG"],
],
),
(
45,
(50, 50),
(20, 20),
(255, 0, 0),
[
["RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR"],
["RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR", "RRRRRR"],
],
),
(
135,
(-100, -100),
None,
(0, 0, 255),
[
["BBBBBB", "BBBBBB", "BBBBBB", "BBBBBB", "BBBBBB"],
["BBBBBB", "BBBBBB", "BBBBBB", "BBBBBB", "BBBBBB"],
],
),
),
)
def test_rotate(
angle_offset,
angle,
unit,
resample,
translate,
center,
bg_color,
expected_frames,
):
"""Check ``rotate`` FX behaviour against possible combinations of arguments."""
original_frames = [["AAAA", "BBBB", "CCCC"], ["ABCD", "BCDE", "CDEA"]]

# angles are defined in degrees, so convert to radians testing ``unit="rad"``
if unit == "rad":
angle = math.radians(angle)

clip = BitmapClip(original_frames, fps=1)

clip3 = rotate(clip, 180 + angle_offset)
target3 = BitmapClip([["CCCC", "BBBB", "AAAA"], ["AEDC", "EDCB", "DCBA"]], fps=1)
assert clip3 == target3
kwargs = {
"unit": unit,
"resample": resample,
"translate": translate,
"center": center,
"bg_color": bg_color,
}
if resample not in ["bilinear", "nearest", "bicubic"]:
with pytest.raises(ValueError) as exc:
clip.rotate(angle, **kwargs)
assert (
"'resample' argument must be either 'bilinear', 'nearest' or 'bicubic'"
) == str(exc.value)
return
else:
rotated_clip = clip.rotate(angle, **kwargs)

clip4 = rotate(clip, 270 + angle_offset)
target4 = BitmapClip(
[["CBA", "CBA", "CBA", "CBA"], ["CBA", "DCB", "EDC", "AED"]], fps=1
)
assert clip4 == target4
expected_clip = BitmapClip(expected_frames, fps=1)
assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()


def test_rotate_nonstandard_angles():
Expand Down

0 comments on commit 487447b

Please sign in to comment.