Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenEXR.OutputFile Fails to Write to BytesIO Buffer #1758

Open
shiukaheng opened this issue May 21, 2024 · 1 comment
Open

OpenEXR.OutputFile Fails to Write to BytesIO Buffer #1758

shiukaheng opened this issue May 21, 2024 · 1 comment

Comments

@shiukaheng
Copy link

Description:

I encountered an issue when trying to use OpenEXR.OutputFile to write to a BytesIO buffer. The operation fails with the following error: OSError: Cannot open image file "". file write failed. It appears that OpenEXR.OutputFile does not handle in-memory buffers as expected.

Steps to Reproduce:

  1. Install OpenEXR and dependencies.
  2. Run the following Python code:
    from io import BytesIO
    import OpenEXR
    import Imath
    import numpy as np
    
    def encode_exr(image: np.array) -> bytes:
        width, height, _ = image.shape
        header = OpenEXR.Header(width, height)
        header['channels'] = {
            'R': Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT)),
            'G': Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT)),
            'B': Imath.Channel(Imath.PixelType(Imath.PixelType.FLOAT))
        }
        header['compression'] = Imath.Compression(Imath.Compression.DWAA_COMPRESSION)
        
        data_r = image[:, :, 0].tobytes()
        data_g = image[:, :, 1].tobytes()
        data_b = image[:, :, 2].tobytes()
        
        buffer = BytesIO()
        exr = OpenEXR.OutputFile(buffer, header)
        exr.writePixels({'R': data_r, 'G': data_g, 'B': data_b})
        exr.close()
    
        return buffer.getvalue()
    
    if __name__ == '__main__':
        random_image = np.random.rand(720, 1280, 3).astype(np.float32)
        exr_bytes = encode_exr(random_image)
  3. Which gives the following error
    Traceback (most recent call last):
      File "script.py", line 30, in <module>
        exr_bytes = encode_exr(random_image)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
      File "script.py", line 22, in encode_exr
        exr = OpenEXR.OutputFile(buffer, header)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    OSError: Cannot open image file "". file write failed
    

Expected Behavior:

OpenEXR.OutputFile should support writing to a BytesIO buffer without raising an error.

I am not very C++ savvy but it seems that the C++ implementation and the python wrapper would handle a byte stream for image writing? Especially when reading from a byte stream is supported.

Appreciate any help. Thanks!

Environment:

  • Python version: 3.11.9
  • OpenEXR version: 3.2.4
  • OS: Linux Mint 21.2
@cary-ilm
Copy link
Member

You're right, the current python bindings only support file I/O, even though the underlying library supports streams.

I'm in the process of entirely rewriting the bindings (#1756), I can add support for byte streams.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants