|
| 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