-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_fx.py
1553 lines (1339 loc) · 45.2 KB
/
test_fx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""MoviePy video and audio effects tests."""
import decimal
import math
import numbers
import os
import random
import numpy as np
import pytest
from moviepy import *
from moviepy.tools import convert_to_seconds
def test_accel_decel():
pass
def test_blackwhite():
# Create black/white spectrum ``bw_color_dict`` to compare against it.
# Colors after ``blackwhite`` FX must be inside this dictionary
# Note: black/white spectrum is made of colors with same numbers
# [(0, 0, 0), (1, 1, 1), (2, 2, 2)...]
bw_color_dict = {}
for num in range(0, 256):
bw_color_dict[chr(num + 255)] = (num, num, num)
color_dict = bw_color_dict.copy()
# update dictionary with default BitmapClip color_dict values
color_dict.update(BitmapClip.DEFAULT_COLOR_DICT)
# add row with random colors in b/w spectrum
random_row = ""
for num in range(512, 515):
# use unique unicode representation for each color
char = chr(num)
random_row += char
# random colors in the b/w spectrum
color_dict[char] = tuple(random.randint(0, 255) for i in range(3))
# clip converted below to black/white
clip = BitmapClip([["RGB", random_row]], color_dict=color_dict, fps=1)
# for each possible ``preserve_luminosity`` boolean argument value
for preserve_luminosity in [True, False]:
# default argument (``RGB=None``)
clip_bw = clip.with_effects(
[vfx.BlackAndWhite(preserve_luminosity=preserve_luminosity)]
)
bitmap = clip_bw.to_bitmap()
assert bitmap
for i, row in enumerate(bitmap[0]):
for char in row:
# all characters returned by ``to_bitmap`` are in the b/w spectrum
assert char in bw_color_dict
if i == 0: # pure "RGB" colors are converted to [85, 85, 85]
assert char == row[0] # so are equal
# custom random ``RGB`` argument
clip_bw_custom_rgb = clip.with_effects(
[
vfx.BlackAndWhite(
RGB=(random.randint(0, 255), 0, 0),
preserve_luminosity=preserve_luminosity,
)
]
)
bitmap = clip_bw_custom_rgb.to_bitmap()
for i, row in enumerate(bitmap[0]):
for i2, char in enumerate(row):
# all characters returned by ``to_bitmap`` are in the b/w spectrum
assert char in bw_color_dict
# for clip "RGB" row, two latest converted colors are equal
if i == 0 and i2 > 0:
assert char == row[1] and char == row[2]
# ``RGB="CRT_phosphor"`` argument
clip_bw_crt_phosphor = clip.with_effects(
[
vfx.BlackAndWhite(
RGB="CRT_phosphor", preserve_luminosity=preserve_luminosity
)
]
)
bitmap = clip_bw_crt_phosphor.to_bitmap()
assert bitmap
for row in bitmap[0]:
for char in row:
# all characters returned by ``to_bitmap`` are in the b/w spectrum
assert char in bw_color_dict
# This currently fails with a with_mask error!
# def test_blink(util):
# with VideoFileClip("media/big_buck_bunny_0_30.webm").subclip(0,10) as clip:
# clip1 = blink(clip, 1, 1)
# clip1.write_videofile(os.path.join(util.TMP_DIR,"blink1.webm"))
def test_multiply_color():
color_dict = {"H": (0, 0, 200), "L": (0, 0, 50), "B": (0, 0, 255), "O": (0, 0, 0)}
clip = BitmapClip([["LLO", "BLO"]], color_dict=color_dict, fps=1)
clipfx = clip.with_effects([vfx.MultiplyColor(4)])
target = BitmapClip([["HHO", "BHO"]], color_dict=color_dict, fps=1)
assert target == clipfx
def test_crop():
# x: 0 -> 4, y: 0 -> 3 inclusive
clip = BitmapClip([["ABCDE", "EDCBA", "CDEAB", "BAEDC"]], fps=1)
clip1 = clip.with_effects([vfx.Crop()])
target1 = BitmapClip([["ABCDE", "EDCBA", "CDEAB", "BAEDC"]], fps=1)
assert clip1 == target1
clip2 = clip.with_effects([vfx.Crop(x1=1, y1=1, x2=3, y2=3)])
target2 = BitmapClip([["DC", "DE"]], fps=1)
assert clip2 == target2
clip3 = clip.with_effects([vfx.Crop(y1=2)])
target3 = BitmapClip([["CDEAB", "BAEDC"]], fps=1)
assert clip3 == target3
clip4 = clip.with_effects([vfx.Crop(x1=2, width=2)])
target4 = BitmapClip([["CD", "CB", "EA", "ED"]], fps=1)
assert clip4 == target4
# TODO x_center=1 does not perform correctly
clip5 = clip.with_effects([vfx.Crop(x_center=2, y_center=2, width=3, height=3)])
target5 = BitmapClip([["ABC", "EDC", "CDE"]], fps=1)
assert clip5 == target5
clip6 = clip.with_effects([vfx.Crop(x_center=2, width=2, y1=1, y2=2)])
target6 = BitmapClip([["DC"]], fps=1)
assert clip6 == target6
def test_even_size():
clip1 = BitmapClip([["ABC", "BCD"]], fps=1) # Width odd
clip1even = clip1.with_effects([vfx.EvenSize()])
target1 = BitmapClip([["AB", "BC"]], fps=1)
assert clip1even == target1
clip2 = BitmapClip([["AB", "BC", "CD"]], fps=1) # Height odd
clip2even = clip2.with_effects([vfx.EvenSize()])
target2 = BitmapClip([["AB", "BC"]], fps=1)
assert clip2even == target2
clip3 = BitmapClip([["ABC", "BCD", "CDE"]], fps=1) # Width and height odd
clip3even = clip3.with_effects([vfx.EvenSize()])
target3 = BitmapClip([["AB", "BC"]], fps=1)
assert clip3even == target3
def test_fadein():
color_dict = {
"I": (0, 0, 0),
"R": (255, 0, 0),
"G": (0, 255, 0),
"B": (0, 0, 255),
"W": (255, 255, 255),
}
clip = BitmapClip([["R"], ["G"], ["B"]], color_dict=color_dict, fps=1)
clip1 = clip.with_effects([vfx.FadeIn(1)]) # default initial color
target1 = BitmapClip([["I"], ["G"], ["B"]], color_dict=color_dict, fps=1)
assert clip1 == target1
clip2 = clip.with_effects(
[vfx.FadeIn(1, initial_color=(255, 255, 255))]
) # different initial color
target2 = BitmapClip([["W"], ["G"], ["B"]], color_dict=color_dict, fps=1)
assert clip2 == target2
def test_fadeout(util, video):
clip = video(end_time=0.5)
clip1 = clip.with_effects([vfx.FadeOut(0.5)])
clip1.write_videofile(os.path.join(util.TMP_DIR, "fadeout1.webm"))
@pytest.mark.parametrize(
(
"t",
"freeze_duration",
"total_duration",
"padding_end",
"output_frames",
),
(
# at start, 1 second (default t == 0)
(
None,
1,
None,
None,
["R", "R", "G", "B"],
),
# at start, 1 second (explicit t)
(
0,
1,
None,
None,
["R", "R", "G", "B"],
),
# at end, 1 second
(
"end",
1,
None,
None,
["R", "G", "B", "B"],
),
# at end 1 second, padding end 1 second
(
"end",
1,
None,
1,
["R", "G", "G", "B"],
),
# at 2nd frame, 1 second
(
1, # second 0 is frame 1, second 1 is frame 2...
1,
None,
None,
["R", "G", "G", "B"],
),
# at 2nd frame, 2 seconds
(
1,
2,
None,
None,
["R", "G", "G", "G", "B"],
),
# `freeze_duration`, `total_duration` are None
(1, None, None, None, ValueError),
# `total_duration` 5 at start (2 seconds)
(None, None, 5, None, ["R", "R", "R", "G", "B"]),
# total duration 5 at end
("end", None, 5, None, ["R", "G", "B", "B", "B"]),
# total duration 5 padding end
("end", None, 5, 1, ["R", "G", "G", "G", "B"]),
),
ids=[
"at start, 1 second (default t == 0)",
"at start, 1 second (explicit t)",
"at end, 1 second",
"at end 1 second, padding end 1 second",
"at 2nd frame, 1 second",
"at 2nd frame, 2 seconds",
"`freeze_duration`, `total_duration` are None",
"`total_duration` 5 at start (2 seconds)",
"`total_duration` 5 at end",
"`total_duration` 5 padding end",
],
)
def test_freeze(t, freeze_duration, total_duration, padding_end, output_frames):
input_frames = ["R", "G", "B"]
clip_duration = len(input_frames)
# create BitmapClip with predefined set of colors, during 1 second each one
clip = BitmapClip([list(color) for color in input_frames], fps=1).with_duration(
clip_duration
)
# build kwargs passed to `freeze`
possible_kwargs = {
"t": t,
"freeze_duration": freeze_duration,
"total_duration": total_duration,
"padding_end": padding_end,
}
kwargs = {
kw_name: kw_value
for kw_name, kw_value in possible_kwargs.items()
if kw_value is not None
}
# freeze clip
if hasattr(output_frames, "__traceback__"):
with pytest.raises(output_frames):
clip.with_effects([vfx.Freeze(**kwargs)])
return
else:
freezed_clip = clip.with_effects([vfx.Freeze(**kwargs)])
# assert new duration
expected_freeze_duration = (
freeze_duration
if freeze_duration is not None
else total_duration - clip_duration
)
assert freezed_clip.duration == clip_duration + expected_freeze_duration
# assert colors are the expected
for i, color in enumerate(freezed_clip.iter_frames()):
expected_color = list(BitmapClip.DEFAULT_COLOR_DICT[output_frames[i]])
assert list(color[0][0]) == expected_color
def test_freeze_region():
clip = BitmapClip([["AAB", "CCC"], ["BBR", "DDD"], ["CCC", "ABC"]], fps=1)
# Test region
clip1 = clip.with_effects([vfx.FreezeRegion(t=1, region=(2, 0, 3, 1))])
target1 = BitmapClip([["AAR", "CCC"], ["BBR", "DDD"], ["CCR", "ABC"]], fps=1)
assert clip1 == target1
# Test outside_region
clip2 = clip.with_effects([vfx.FreezeRegion(t=1, outside_region=(2, 0, 3, 1))])
target2 = BitmapClip([["BBB", "DDD"], ["BBR", "DDD"], ["BBC", "DDD"]], fps=1)
assert clip2 == target2
def test_gamma_corr():
pass
def test_headblur():
pass
def test_invert_colors():
clip = BitmapClip(
[["AB", "BC"]],
color_dict={"A": (0, 0, 0), "B": (50, 100, 150), "C": (255, 255, 255)},
fps=1,
)
clip1 = clip.with_effects([vfx.InvertColors()])
target1 = BitmapClip(
[["CD", "DA"]],
color_dict={"A": (0, 0, 0), "D": (205, 155, 105), "C": (255, 255, 255)},
fps=1,
)
assert clip1 == target1
def test_loop(util, video):
clip = BitmapClip([["R"], ["G"], ["B"]], fps=1)
clip1 = clip.with_effects([vfx.Loop(n=2)]) # loop 2 times
target1 = BitmapClip([["R"], ["G"], ["B"], ["R"], ["G"], ["B"]], fps=1)
assert clip1 == target1
clip2 = clip.with_effects([vfx.Loop(duration=8)]) # loop 8 seconds
target2 = BitmapClip(
[["R"], ["G"], ["B"], ["R"], ["G"], ["B"], ["R"], ["G"]], fps=1
)
assert clip2 == target2
clip3 = clip.with_effects([vfx.Loop()]).with_duration(5) # infinite loop
target3 = BitmapClip([["R"], ["G"], ["B"], ["R"], ["G"]], fps=1)
assert clip3 == target3
clip = video(start_time=0.2, end_time=0.3) # 0.1 seconds long
clip1 = clip.with_effects([vfx.Loop()]).with_duration(0.5) # infinite looping
clip1.write_videofile(os.path.join(util.TMP_DIR, "loop1.webm"))
clip2 = clip.with_effects([vfx.Loop(duration=0.5)]) # loop for 1 second
clip2.write_videofile(os.path.join(util.TMP_DIR, "loop2.webm"))
clip3 = clip.with_effects([vfx.Loop(n=3)]) # loop 3 times
clip3.write_videofile(os.path.join(util.TMP_DIR, "loop3.webm"))
# Test audio looping
clip = AudioClip(
lambda t: np.sin(440 * 2 * np.pi * t) * (t % 1) + 0.5, duration=2.5, fps=44100
)
clip1 = clip.with_effects([vfx.Loop(2)])
# TODO fix AudioClip.__eq__()
# assert concatenate_audioclips([clip, clip]) == clip1
def test_lum_contrast(util, video):
clip = video()
clip1 = clip.with_effects([vfx.LumContrast()])
clip1.write_videofile(os.path.join(util.TMP_DIR, "lum_contrast1.webm"))
# what are the correct value ranges for function arguments lum,
# contrast and contrast_thr? Maybe we should check for these in
# lum_contrast.
def test_make_loopable(util, video):
clip = video()
clip1 = clip.with_effects([vfx.MakeLoopable(0.4)])
# We need to set libvpx-vp9 because our test will produce transparency
clip1.write_videofile(
os.path.join(util.TMP_DIR, "make_loopable1.webm"), codec="libvpx-vp9"
)
@pytest.mark.parametrize(
("ClipClass"),
(ColorClip, BitmapClip),
ids=("ColorClip", "BitmapClip"),
)
@pytest.mark.parametrize(
(
"margin_size",
"margins", # [left, right, top, bottom]
"color",
"expected_result",
),
(
pytest.param(
None,
None,
None,
[["RRR", "RRR"], ["RRR", "RRR"]],
id="default arguments",
),
pytest.param(
1,
None,
None,
[
["OOOOO", "ORRRO", "ORRRO", "OOOOO"],
["OOOOO", "ORRRO", "ORRRO", "OOOOO"],
],
id="margin_size=1,color=(0, 0, 0)",
),
pytest.param(
1,
None,
(0, 255, 0),
[
["GGGGG", "GRRRG", "GRRRG", "GGGGG"],
["GGGGG", "GRRRG", "GRRRG", "GGGGG"],
],
id="margin_size=1,color=(0, 255, 0)",
),
pytest.param(
None,
[1, 0, 0, 0],
(0, 255, 0),
[["GRRR", "GRRR"], ["GRRR", "GRRR"]],
id="left=1,color=(0, 255, 0)",
),
pytest.param(
None,
[0, 1, 0, 0],
(0, 255, 0),
[["RRRG", "RRRG"], ["RRRG", "RRRG"]],
id="right=1,color=(0, 255, 0)",
),
pytest.param(
None,
[1, 0, 1, 0],
(0, 255, 0),
[["GGGG", "GRRR", "GRRR"], ["GGGG", "GRRR", "GRRR"]],
id="left=1,top=1,color=(0, 255, 0)",
),
pytest.param(
None,
[0, 1, 1, 1],
(0, 255, 0),
[["GGGG", "RRRG", "RRRG", "GGGG"], ["GGGG", "RRRG", "RRRG", "GGGG"]],
id="right=1,top=1,bottom=1,color=(0, 255, 0)",
),
pytest.param(
None,
[3, 0, 0, 0],
(255, 255, 255),
[["WWWRRR", "WWWRRR"], ["WWWRRR", "WWWRRR"]],
id="left=3,color=(255, 255, 255)",
),
pytest.param(
None,
[0, 0, 0, 4],
(255, 255, 255),
[
["RRR", "RRR", "WWW", "WWW", "WWW", "WWW"],
["RRR", "RRR", "WWW", "WWW", "WWW", "WWW"],
],
id="bottom=4,color=(255, 255, 255)",
),
),
)
def test_margin(ClipClass, margin_size, margins, color, expected_result):
if ClipClass is BitmapClip:
clip = BitmapClip([["RRR", "RRR"], ["RRR", "RRR"]], fps=1)
else:
clip = ColorClip(color=(255, 0, 0), size=(3, 2), duration=2).with_fps(1)
# if None, set default argument values
if color is None:
color = (0, 0, 0)
if margins is None:
margins = [0, 0, 0, 0]
left, right, top, bottom = margins
new_clip = clip.with_effects(
[
vfx.Margin(
margin_size=margin_size,
left=left,
right=right,
top=top,
bottom=bottom,
color=color,
)
]
)
assert new_clip == BitmapClip(expected_result, fps=1)
@pytest.mark.parametrize("image_from", ("np.ndarray", "ImageClip"))
@pytest.mark.parametrize("duration", (None, "random"))
@pytest.mark.parametrize(
("color", "mask_color", "expected_color"),
(
(
(0, 0, 0),
(255, 255, 255),
(0, 0, 0),
),
(
(255, 0, 0),
(0, 0, 255),
(0, 0, 0),
),
(
(255, 255, 255),
(0, 10, 20),
(0, 10, 20),
),
(
(10, 10, 10),
(20, 0, 20),
(10, 0, 10),
),
),
)
def test_mask_and(image_from, duration, color, mask_color, expected_color):
"""Checks ``mask_and`` FX behaviour."""
clip_size = tuple(random.randint(3, 10) for i in range(2))
if duration == "random":
duration = round(random.uniform(0, 0.5), 2)
# test ImageClip and np.ndarray types as mask argument
clip = ColorClip(color=color, size=clip_size).with_duration(duration)
mask_clip = ColorClip(color=mask_color, size=clip.size)
masked_clip = clip.with_effects(
[
vfx.MasksAnd(
mask_clip if image_from == "ImageClip" else mask_clip.get_frame(0)
)
]
)
assert masked_clip.duration == clip.duration
assert np.array_equal(masked_clip.get_frame(0)[0][0], np.array(expected_color))
# test VideoClip as mask argument
color_frame, mask_color_frame = (np.array([[color]]), np.array([[mask_color]]))
clip = VideoClip(lambda t: color_frame).with_duration(duration)
mask_clip = VideoClip(lambda t: mask_color_frame).with_duration(duration)
masked_clip = clip.with_effects([vfx.MasksAnd(mask_clip)])
assert np.array_equal(masked_clip.get_frame(0)[0][0], np.array(expected_color))
def test_mask_color():
pass
@pytest.mark.parametrize("image_from", ("np.ndarray", "ImageClip"))
@pytest.mark.parametrize("duration", (None, "random"))
@pytest.mark.parametrize(
("color", "mask_color", "expected_color"),
(
(
(0, 0, 0),
(255, 255, 255),
(255, 255, 255),
),
(
(255, 0, 0),
(0, 0, 255),
(255, 0, 255),
),
(
(255, 255, 255),
(0, 10, 20),
(255, 255, 255),
),
(
(10, 10, 10),
(20, 0, 20),
(20, 10, 20),
),
),
)
def test_mask_or(image_from, duration, color, mask_color, expected_color):
"""Checks ``mask_or`` FX behaviour."""
clip_size = tuple(random.randint(3, 10) for i in range(2))
if duration == "random":
duration = round(random.uniform(0, 0.5), 2)
# test ImageClip and np.ndarray types as mask argument
clip = ColorClip(color=color, size=clip_size).with_duration(duration)
mask_clip = ColorClip(color=mask_color, size=clip.size)
masked_clip = clip.with_effects(
[
vfx.MasksOr(
mask_clip if image_from == "ImageClip" else mask_clip.get_frame(0)
)
]
)
assert masked_clip.duration == clip.duration
assert np.array_equal(masked_clip.get_frame(0)[0][0], np.array(expected_color))
# test VideoClip as mask argument
color_frame, mask_color_frame = (np.array([[color]]), np.array([[mask_color]]))
clip = VideoClip(lambda t: color_frame).with_duration(duration)
mask_clip = VideoClip(lambda t: mask_color_frame).with_duration(duration)
masked_clip = clip.with_effects([vfx.MasksOr(mask_clip)])
assert np.array_equal(masked_clip.get_frame(0)[0][0], np.array(expected_color))
def test_mirror_x():
clip = BitmapClip([["AB", "CD"]], fps=1)
clip1 = clip.with_effects([vfx.MirrorX()])
target = BitmapClip([["BA", "DC"]], fps=1)
assert clip1 == target
def test_mirror_y():
clip = BitmapClip([["AB", "CD"]], fps=1)
clip1 = clip.with_effects([vfx.MirrorY()])
target = BitmapClip([["CD", "AB"]], fps=1)
assert clip1 == target
def test_painting():
pass
@pytest.mark.parametrize("apply_to_mask", (True, False))
@pytest.mark.parametrize(
(
"size",
"duration",
"new_size",
"width",
"height",
),
(
(
[8, 2],
1,
[4, 1],
None,
None,
),
(
[8, 2],
1,
None,
4,
None,
),
(
[2, 8],
1,
None,
None,
4,
),
# neither 'new_size', 'height' or 'width'
(
[2, 2],
1,
None,
None,
None,
),
# `new_size` as scaling factor
(
[5, 5],
1,
2,
None,
None,
),
(
[5, 5],
1,
decimal.Decimal(2.5),
None,
None,
),
# arguments as functions
(
[2, 2],
4,
lambda t: {0: [4, 4], 1: [8, 8], 2: [11, 11], 3: [5, 8]}[t],
None,
None,
),
(
[2, 4],
2,
None,
None,
lambda t: {0: 3, 1: 4}[t],
),
(
[5, 2],
2,
None,
lambda t: {0: 3, 1: 4}[t],
None,
),
),
)
def test_resize(apply_to_mask, size, duration, new_size, height, width):
"""Checks ``resize`` FX behaviours using all argument"""
# build expected sizes (using `width` or `height` arguments will be proportional
# to original size)
if new_size:
if hasattr(new_size, "__call__"):
# function
expected_new_sizes = [new_size(t) for t in range(duration)]
elif isinstance(new_size, numbers.Number):
# scaling factor
expected_new_sizes = [[int(size[0] * new_size), int(size[1] * new_size)]]
else:
# tuple or list
expected_new_sizes = [new_size]
elif height:
if hasattr(height, "__call__"):
expected_new_sizes = []
for t in range(duration):
new_height = height(t)
expected_new_sizes.append(
[int(size[0] * new_height / size[1]), new_height]
)
else:
expected_new_sizes = [[size[0] * height / size[1], height]]
elif width:
if hasattr(width, "__call__"):
expected_new_sizes = []
for t in range(duration):
new_width = width(t)
expected_new_sizes.append(
[new_width, int(size[1] * new_width / size[0])]
)
else:
expected_new_sizes = [[width, size[1] * width / size[0]]]
else:
expected_new_sizes = None
clip = ColorClip(size=size, color=(0, 0, 0), duration=duration)
clip.fps = 1
mask = ColorClip(size=size, color=0, is_mask=True)
clip = clip.with_mask(mask)
# any resizing argument passed, raises `ValueError`
if expected_new_sizes is None:
with pytest.raises(ValueError):
resized_clip = clip.resized(
new_size=new_size,
height=height,
width=width,
apply_to_mask=apply_to_mask,
)
resized_clip = clip
expected_new_sizes = [size]
else:
resized_clip = clip.resized(
new_size=new_size, height=height, width=width, apply_to_mask=apply_to_mask
)
# assert new size for each frame
for t in range(duration):
expected_width = expected_new_sizes[t][0]
expected_height = expected_new_sizes[t][1]
clip_frame = resized_clip.get_frame(t)
assert len(clip_frame[0]) == expected_width
assert len(clip_frame) == expected_height
mask_frame = resized_clip.mask.get_frame(t)
if apply_to_mask:
assert len(mask_frame[0]) == expected_width
assert len(mask_frame) == expected_height
@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"]],
),
(
lambda t: 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,
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":
if hasattr(angle, "__call__"):
_angle = lambda t: math.radians(angle(0))
else:
_angle = math.radians(angle)
else:
_angle = angle
clip = BitmapClip(original_frames, fps=1)
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.rotated(_angle, **kwargs)
assert (
"'resample' argument must be either 'bilinear', 'nearest' or 'bicubic'"
) == str(exc.value)
return
# resolve the angle, because if it is a multiple of 90, the rotation
# can be computed event without an available PIL installation
if hasattr(_angle, "__call__"):
_resolved_angle = _angle(0)
else:
_resolved_angle = _angle
if unit == "rad":
_resolved_angle = math.degrees(_resolved_angle)
rotated_clip = clip.with_effects([vfx.Rotate(_angle, **kwargs)])
expected_clip = BitmapClip(expected_frames, fps=1)
assert rotated_clip.to_bitmap() == expected_clip.to_bitmap()
def test_rotate_nonstandard_angles(util):
# Test rotate with color clip
clip = ColorClip([600, 400], [150, 250, 100]).with_duration(1).with_fps(5)
clip = clip.with_effects([vfx.Rotate(20)])
clip.write_videofile(os.path.join(util.TMP_DIR, "color_rotate.webm"))
def test_rotate_mask():
# Prior to https://github.com/Zulko/moviepy/pull/1399
# all the pixels of the resulting video were 0
clip = (
ColorClip(color=0.5, size=(1, 1), is_mask=True)
.with_fps(1)
.with_duration(1)
.with_effects([vfx.Rotate(45)])
)
assert clip.get_frame(0)[1][1] != 0
@pytest.mark.parametrize(
("unsupported_kwargs",),
(
(["bg_color"],),
(["center"],),
(["translate"],),
(["translate", "center"],),
(["center", "bg_color", "translate"],),
),
ids=(
"bg_color",
"center",
"translate",
"translate,center",
"center,bg_color,translate",
),
)
def test_rotate_supported_PIL_kwargs(
unsupported_kwargs,
monkeypatch,
):
"""Test supported 'rotate' FX arguments by PIL version."""
pass
def test_scroll():
pass
def test_multiply_speed():
clip = BitmapClip([["A"], ["B"], ["C"], ["D"]], fps=1)
clip1 = clip.with_effects([vfx.MultiplySpeed(0.5)]) # 1/2x speed
target1 = BitmapClip(
[["A"], ["A"], ["B"], ["B"], ["C"], ["C"], ["D"], ["D"]], fps=1
)
assert clip1 == target1
clip2 = clip.with_effects([vfx.MultiplySpeed(final_duration=8)]) # 1/2x speed
target2 = BitmapClip(
[["A"], ["A"], ["B"], ["B"], ["C"], ["C"], ["D"], ["D"]], fps=1