-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtest_rawdata_pillow.py
More file actions
95 lines (63 loc) · 2.4 KB
/
test_rawdata_pillow.py
File metadata and controls
95 lines (63 loc) · 2.4 KB
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
from os import path
from tempfile import TemporaryDirectory
import numpy as np
import pytest
from PIL import Image, ImageSequence
import ffmpegio as ff
from ffmpegio import plugins
def comp_images(im1, im2):
return (
im1.mode == im2.mode and im1.size == im2.size and im1.tobytes() == im2.tobytes()
)
# test only with the base plugins
@pytest.fixture(scope="module", autouse=True)
def use_pillow():
ff.use("read_pillow")
assert ff.using("video") == "read_pillow"
yield
ff.use("read_bytes")
@pytest.fixture(scope="module")
def shape():
return (120, 100)
@pytest.fixture(scope="module")
def base_image(shape):
return Image.fromarray(np.random.randint(0, 256, (*shape, 4), np.uint8), "RGBA")
@pytest.mark.parametrize(
"mode",
["L", "RGB", "RGBA", "F", "LA", "I;16L", "I;16B"],
)
def test_pillow_image(base_image, shape, mode):
# create random test Image object
image = base_image.convert(mode=mode)
hook = plugins.get_hook()
shape_in, dtype_in = hook.video_info(obj=image)
assert shape == shape_in[:-1]
b = hook.video_bytes(obj=image)
image_out = hook.bytes_to_video(b=b, dtype=dtype_in, shape=shape_in, squeeze=False)
assert comp_images(image_out[0], image)
def test_pillow_image_seq(shape):
images = [
Image.fromarray(np.random.randint(0, 256, (*shape, 4), np.uint8), "RGBA")
for _ in range(10)
]
hook = plugins.get_hook()
shape_in, dtype_in = hook.video_info(obj=images)
b = hook.video_bytes(obj=images)
image_out = hook.bytes_to_video(b=b, dtype=dtype_in, shape=shape_in, squeeze=False)
for fin, fout in zip(images, image_out):
assert comp_images(fin, fout)
@pytest.mark.skip(reason="Not supported on all platform???")
def test_pillow_avif():
url = "tests/assets/testvideo-1m.mp4"
hook = plugins.get_hook()
with TemporaryDirectory() as tmpdir:
avifpath = path.join(tmpdir, "test.avif")
ff.transcode(url, avifpath, f="avif", r=1, to=10, show_log=True)
with Image.open(avifpath, formats=["AVIF"]) as image:
shape_in, dtype_in = hook.video_info(obj=image)
b = hook.video_bytes(obj=image)
image_out = hook.bytes_to_video(
b=b, dtype=dtype_in, shape=shape_in, squeeze=False
)
for fin, fout in zip(ImageSequence.Iterator(image), image_out):
assert comp_images(fin, fout)