Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate argument's combinations #1116

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions moviepy/video/fx/crop.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def crop(
clip,
*,
x1=None,
y1=None,
x2=None,
Expand Down Expand Up @@ -29,8 +30,7 @@ def crop(

Crop a rectangle centered in x,y=(300,400), width=50, height=150 :

>>> crop(clip, x_center=300 , y_center=400,
width=50, height=150)
>>> crop(clip, x_center=300, y_center=400, width=50, height=150)

Any combination of the above should work, like for this rectangle
centered in x=300, with explicit y-boundaries:
Expand All @@ -40,26 +40,34 @@ def crop(
"""

if width and x1 is not None:
assert not x2, "If width and x1 is given, x2 should be None"
x2 = x1 + width
elif width and x2 is not None:
assert not x1, "If width and x2 is given, x1 should be None"
x1 = x2 - width

if height and y1 is not None:
assert not y2, "If height and y1 is given, y2 should be None"
y2 = y1 + height
elif height and y2 is not None:
assert not y1, "If height and y2 is given, y1 should be None"
y1 = y2 - height

if x_center:
assert width, "If x_center is given, width should be given"
assert not x1 and not x2, "If x_center is given, x1 and x2 should be None"
x1, x2 = x_center - width / 2, x_center + width / 2

if y_center:
assert height, "If y_center is given, height should be given"
assert not y1 and not y2, "If y_center is given, y1 and y2 should be None"
y1, y2 = y_center - height / 2, y_center + height / 2

x1 = x1 or 0
y1 = y1 or 0
x2 = x2 or clip.size[0]
y2 = y2 or clip.size[1]

return clip.fl_image(
lambda pic: pic[int(y1) : int(y2), int(x1) : int(x2)], apply_to=["mask"]
)
def crop_frame(frame):
return frame[int(y1) : int(y2), int(x1) : int(x2)]

return clip.fl_image(crop_frame, apply_to=["mask"])