Skip to content

Commit f904ce8

Browse files
committed
test: EXR File에 대한 읽기 및 쓰기 Test를 추가하다
test_read: exrfile을 읽고, 모든 채널을 numpy array로 변환하고 기존 header와 동일한 크기인지 비교 test_write: 새로운 header를 만들고 여러 채널을 추가한 뒤 exr 파일로 내보내고, 생성이 완료되면 삭제
1 parent b1e225d commit f904ce8

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

tests/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .test_import import *
1+
from .test_readwrite import *

tests/asset/Balls.exr

9.67 MB
Binary file not shown.

tests/test_import.py

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/test_readwrite.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
from typing import Type
2+
import numpy as np
3+
import pytest
4+
import openexr_python as exr
5+
import os
6+
import Imath
7+
8+
ASSET_DIR = os.path.join(os.path.dirname(__file__), "asset")
9+
10+
11+
@pytest.mark.parametrize(
12+
[
13+
"file_path",
14+
],
15+
[
16+
(os.path.join(ASSET_DIR, asset_name),)
17+
for asset_name in [
18+
"Balls.exr",
19+
]
20+
],
21+
)
22+
def test_read(file_path):
23+
input_file = exr.EXRInputFile(file_path=file_path)
24+
25+
header = input_file.header
26+
channels = header.channels
27+
imgs = [input_file.read_numpy(c) for c in channels.keys()]
28+
size = exr.get_size_from_window(header.data_window)
29+
for img in imgs:
30+
h, w = img.shape
31+
assert (w, h) == size
32+
33+
34+
def test_write():
35+
header = exr.EXRHeader.create_header(640, 480)
36+
for c in [
37+
"TestLayerRGBA.R",
38+
"TestLayerRGBA.G",
39+
"TestLayerRGBA.B",
40+
"TestLayerRGBA.A",
41+
"TestLayerRGB.R",
42+
"TestLayerRGB.G",
43+
"TestLayerRGB.B",
44+
"TestLayerR.R",
45+
"TestLayerGB.G",
46+
"TestLayerGB.B",
47+
"velocity.x",
48+
"velocity.y",
49+
"velocity.z",
50+
"depth.z",
51+
"adsad.x",
52+
"adsad.y",
53+
"adsad.z",
54+
]:
55+
header.add_channel(c, exr.create_channel(np.float16))
56+
path = os.path.join(ASSET_DIR, "temp_openexr_for_test.exr")
57+
output_file = exr.EXROutputFile(file_path=path, header=header)
58+
pixels = {}
59+
for c_key in header.channels.keys():
60+
dtype = exr.convert_imath_type_to_numpy_type(header.channels[c_key].type)
61+
size = exr.get_size_from_window(header.data_window)
62+
numpy_buffer = np.zeros(shape=(size[1], size[0]), dtype=dtype)
63+
for j in range(size[1]):
64+
for i in range(size[0]):
65+
numpy_buffer[j, i] = (
66+
1.0 * (i / size[0]) * (j / size[1]) * np.random.random()
67+
)
68+
pixels[c_key] = numpy_buffer
69+
output_file.write_pixels_with_numpy(pixels=pixels)
70+
assert os.path.exists(path=path)
71+
os.remove(path=path)
72+
assert not os.path.exists(path=path)

0 commit comments

Comments
 (0)