AcademySoftwareFoundation / AcademySoftwareFoundation/openexr

Writing strided numpy arrays

Open
#1,929 1 comment 1 reaction 1 assignee Claimed by @cary-ilm View on GitHub
needs attention
Dominant language
C
Stars
1.8k
Forks
700
Avg merge
3d 12h
Merged PRs (30d)
29

Description

There seems to be an issue when writing numpy arrays with non-standard strides. Consider the following example of me writing a multi-channel image
```import OpenEXR
import numpy as np
shape = (4,4)

channels_np = np.zeros((*shape, 10), dtype=np.float32)
channels_np += np.arange(10)
channels = {}
for i in range(channels_np.shape[-1]):
channels[f"chan{i}"] = channels_np[:, :, i]

print("chan1 before writing\n", channels["chan1"])
header = {"compression" : OpenEXR.RLE_COMPRESSION, "type" : OpenEXR.scanlineimage}
with OpenEXR.File(header, channels) as outfile:
outfile.write("tmp.exr")

with OpenEXR.File("tmp.exr") as infile:
print("chan1 read from file\n", infile.channels()["chan1"].pixels)
```

This prints
```
chan1 before writing
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
chan1 read from file
[[1. 2. 3. 4.]
[5. 6. 7. 8.]
[9. 0. 1. 2.]
[3. 4. 5. 6.]]
```
If I `.copy()` the arrays before adding them to the `channels` dict, this goes away, but it took me a long time to track down the error.
Ideally it should perform this copy internally if necessary, or at least warn the user that the strides are not supported.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.