Skip to content

Commit 6ae22db

Browse files
test(image): add unit tests for _to_image_tensor (#281)
* test(image): add unit tests for _to_image_tensor * style(image): use single-quote strings * chore: rename variable Co-authored-by: AlaeddineAbdessalem <alaeddine-13@live.fr>
1 parent 49e810e commit 6ae22db

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

tests/unit/document/test_image.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
3+
import numpy as np
4+
import pytest
5+
from PIL import Image
6+
import io
7+
8+
from docarray.document.mixins.image import _to_image_tensor
9+
10+
11+
@pytest.mark.parametrize(
12+
'width, height, output_shape',
13+
[
14+
(None, None, (50, 10, 3)),
15+
(8, None, (50, 8, 3)),
16+
(None, 30, (30, 10, 3)),
17+
(30, 8, (8, 30, 3)),
18+
],
19+
)
20+
def test_to_image_tensor_pil(rgb_image_path, width, height, output_shape):
21+
tensor = _to_image_tensor(rgb_image_path, width, height)
22+
23+
assert tensor.shape == output_shape
24+
assert isinstance(tensor, np.ndarray)
25+
26+
27+
def test_to_image_tensor_blob(rgb_image_path):
28+
with open(rgb_image_path, 'rb') as f:
29+
blob = io.BytesIO(f.read())
30+
31+
tensor = _to_image_tensor(blob, None, None)
32+
33+
assert tensor.shape == (50, 10, 3)
34+
assert isinstance(tensor, np.ndarray)
35+
36+
37+
@pytest.fixture
38+
def rgb_image_path(tmpdir):
39+
img_path = os.path.join(tmpdir, 'image.png')
40+
image = Image.new('RGB', size=(10, 50), color=(0, 0, 0))
41+
image.save(img_path)
42+
return img_path

0 commit comments

Comments
 (0)