GithubHelp home page GithubHelp logo

pyexr's Introduction

Python compat PyPI codecov

PyEXR

A simple EXR IO-library for Python that simplifies the use of OpenEXR.

Installation

pip install pyexr

Reading

Simple files

import pyexr

with pyexr.open("color.exr") as file:
    file.channels               # [R, G, B]
    file.width                  # 1280
    file.height                 # 720
    file.channel_precision["R"] # pyexr.FLOAT

    img = file.get()                         # (720,1280,3) np.float32 array
    img = file.get(precision=pyexr.HALF)          # (720,1280,3) np.float16 array
    red = file.get("R")                  # (720,1280,1) np.float32 array
    red = file.get("R", precision=pyexr.HALF) # (720,1280,1) np.float16 array

Fat / Multi-channel EXRs

import pyexr

with pyexr.open("multi-channel.exr") as file:
    file.channels               # [R, G, B, A, Variance.R, Variance.G, Variance.B]
    file.width                  # 1280
    file.height                 # 720

    all = file.get()            # (720,1280,7) np.float32 array (R,G,B,A,Var..)
    var = file.get("Variance")  # (720,1280,3) np.float32 array
    col = file.get("default")   # (720,1280,4) np.float32 array (R,G,B,A)
    file.channel_map['default'] # ['R','G','B','A']

    var_r = file.channel("Variance.R") # (720,1280,3) np.float32 array

One line reading

import pyexr

# 'color.exr' contains R, G, B
img = pyexr.read("color.exr")                  # (720,1280,3) np.float32 array
img = pyexr.read("color.exr", precision=pyexr.HALF) # (720,1280,3) np.float16 array

# 'multi-channel.exr' contains R, G, B, A, Variance.R, Variance.G, Variance.B
all = pyexr.read("multi-channel.exr")             # (720,1280,7) np array
col = pyexr.read("multi-channel.exr", "default")  # (720,1280,4) np array
var = pyexr.read("multi-channel.exr", "Variance") # (720,1280,3) np array

col, var = pyexr.read("multi-channel.exr", ["default", "Variance"])
col, var = pyexr.read("multi-channel.exr", ["default", "Variance"], precision=pyexr.HALF)
col, var = pyexr.read("multi-channel.exr", ["default", "Variance"], precision=[pyexr.HALF, pyexr.FLOAT])

Writing

You can write a matrix to EXR without specifying channel names. Default channel names will then be used:

# columns default
1 channel Z
2 channels X, Y
3 channels R, G, B
4 channels R, G, B, A
import pyexr

depth  # (720,1280) np.float16 array
color  # (720,180,3) np.float32 array
normal # (720,180,3) np.float32 array

pyexr.write("out.exr", depth)            # one FLOAT channel: Z
pyexr.write("out.exr", color)            # three FLOAT channels: R, G, B
pyexr.write("out.exr", normal,
          channel_names=['X','Y','Z']) # three FLOAT channels: X, Y, Z

Writing Fat EXRs

import pyexr

depth    # (720,1280) np.float16 array
color    # (720,180,3) np.float32 array
variance # (720,180,3) np.float32 array

data = {'default': color, 'Depth': depth, 'Variance': variance} # default is a reserved name

pyexr.write("out.exr", data) # channels R, G, B, Depth.Z, Variance.(R,G,B)

# Full customization:
pyexr.write(
    "out.exr",
    data,
    precision = {'default': pyexr.HALF},
    channel_names = {'Depth': ['Q']}
) # channels R, G, B, Depth.Q, Variance.R, Variance.G, Variance.B

pyexr's People

Contributors

brianpugh avatar chaosink avatar justinasdz avatar r1cc4rdo avatar shuomikan avatar smerzbach avatar toaarnio avatar tvogels avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pyexr's Issues

Written files can't be opened by other programs

I don't know if I'm doing something wrong. Opening files written by pyexr in other programs causes weird behaviour.

import numpy as np
import pyexr
import imageio

pyexr.write("test.exr", np.asarray([[0,1],[2,3]], dtype=np.float32))
print(pyexr.read("test.exr"))
print(imageio.imread("test.exr"))

The Output is:

[[[0.]
  [1.]]

 [[2.]
  [3.]]]
WARNING:root:imageio.freeimage warning: Warning: loading color model Z as Y color model
[[0. 0.]
 [0. 0.]]

https://github.com/tvogels/pyexr/files/5156542/test.zip

I also tried gimp, but it refuses completely to open the files, saying that the corresponding plugin crashed.

Edit: Using the 'Y'-channel for writing, reading the file back in works, but because of the different color-interpretation I won't consider this as a usable workaround. Maybe the problem has to be searched in the OpenEXR python-library.

Release to PyPi

Hi @tvogels ,

I was wondering if you could bump the version and release the new version of pyexr to pypi? Thanks!

installation on mac using conda

In case anyone has trouble installing pyexr on mac, I found the following commands work well in my case.

brew install openexr
conda install -c conda-forge openexr-python
pip install pyexr

Building whell for OpenExr (Setup.py) error

Hi, I got the below error message when I install pyexr on Windows 10 with conda, and I have installed OpenEXR. is there any solution to this? Thanks a lot.

(openexr) C:\Users\CHENN>pip install pyexr
Processing c:\users\chenn\appdata\local\pip\cache\wheels\03\b7\93\fb457a606b9a72af7fcdf620cc8c0ba10421c89b39c411240e\pyexr-0.3.7-py2.py3-none-any.whl
Processing c:\users\chenn\appdata\local\pip\cache\wheels\8b\99\a0\81daf51dcd359a9377b110a8a886b3895921802d2fc1b2397e\future-0.18.2-cp36-none-any.whl
Collecting OpenEXR
  Using cached OpenEXR-1.3.2.tar.gz (10 kB)
Collecting numpy
  Downloading numpy-1.18.2-cp36-cp36m-win_amd64.whl (12.8 MB)
     |████████████████████████████████| 12.8 MB 6.8 MB/s
Building wheels for collected packages: OpenEXR
  Building wheel for OpenEXR (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'D:\Anaconda3\envs\openexr\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"'; __file__='"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\CHENN\AppData\Local\Temp\pip-wheel-c95sgsnx'
       cwd: C:\Users\CHENN\AppData\Local\Temp\pip-install-q1mffmic\OpenEXR\
  Complete output (15 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-3.6
  copying Imath.py -> build\lib.win-amd64-3.6
  running build_ext
  building 'OpenEXR' extension
  creating build\temp.win-amd64-3.6
  creating build\temp.win-amd64-3.6\Release
  c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -I/usr/include/OpenEXR -I/usr/local/include/OpenEXR -I/opt/local/include/OpenEXR -ID:\Anaconda3\envs\openexr\include -ID:\Anaconda3\envs\openexr\include "-Ic:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-Ic:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /EHsc /TpOpenEXR.cpp /Fobuild\temp.win-amd64-3.6\Release\OpenEXR.obj -g -DVERSION="1.3.2"
  cl : Command line warning D9002 : ignoring unknown option '-g'
  OpenEXR.cpp
  OpenEXR.cpp(36): fatal error C1083: Cannot open include file: 'ImathBox.h': No such file or directory
  error: command 'c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
  ----------------------------------------
  ERROR: Failed building wheel for OpenEXR
  Running setup.py clean for OpenEXR
Failed to build OpenEXR
ERROR: pymanopt 0.2.4 requires scipy>=0.17, which is not installed.
Installing collected packages: future, OpenEXR, numpy, pyexr
    Running setup.py install for OpenEXR ... error
    ERROR: Command errored out with exit status 1:
     command: 'D:\Anaconda3\envs\openexr\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"'; __file__='"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\CHENN\AppData\Local\Temp\pip-record-ywt2yr3c\install-record.txt' --single-version-externally-managed --compile --install-headers 'D:\Anaconda3\envs\openexr\Include\OpenEXR'
         cwd: C:\Users\CHENN\AppData\Local\Temp\pip-install-q1mffmic\OpenEXR\
    Complete output (15 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.6
    copying Imath.py -> build\lib.win-amd64-3.6
    running build_ext
    building 'OpenEXR' extension
    creating build\temp.win-amd64-3.6
    creating build\temp.win-amd64-3.6\Release
    c:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -I/usr/include/OpenEXR -I/usr/local/include/OpenEXR -I/opt/local/include/OpenEXR -ID:\Anaconda3\envs\openexr\include -ID:\Anaconda3\envs\openexr\include "-Ic:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\ATLMFC\include" "-Ic:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include" "-IC:\Program Files (x86)\Windows Kits\NETFXSDK\4.6.1\include\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\cppwinrt" /EHsc /TpOpenEXR.cpp /Fobuild\temp.win-amd64-3.6\Release\OpenEXR.obj -g -DVERSION="1.3.2"
    cl : Command line warning D9002 : ignoring unknown option '-g'
    OpenEXR.cpp
    OpenEXR.cpp(36): fatal error C1083: Cannot open include file: 'ImathBox.h': No such file or directory
    error: command 'c:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\VC\\Tools\\MSVC\\14.24.28314\\bin\\HostX86\\x64\\cl.exe' failed with exit status 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'D:\Anaconda3\envs\openexr\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"'; __file__='"'"'C:\\Users\\CHENN\\AppData\\Local\\Temp\\pip-install-q1mffmic\\OpenEXR\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\CHENN\AppData\Local\Temp\pip-record-ywt2yr3c\install-record.txt' --single-version-externally-managed --compile --install-headers 'D:\Anaconda3\envs\openexr\Include\OpenEXR' Check the logs for full command output.

Adding header interface

I think it would be great to be able to read and write the EXR header from pyexr. In particular, I'd like to be able to access the header attributes and add custom attributes.

Would you be interested in a patch with this feature? I think it could be implemented quite simply as an optional feature for users who need it without cluttering the basic interface.

lower or upper string channel name

Hi, Could you help me to modify your script? I would like to specify if a channel should be named with a lower or upper letter after the dot.

example :
albedo.r or albedo.R

Thanks

np.fromstring is deprecated

np.fromstring here is deprecated:

/usr/local/lib/python3.6/dist-packages/pyexr/exr.py:241: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
    np.fromstring(strings[i], dtype = precision) \

It looks like there are slight nuances between fromstring and from buffer (read only, perhaps byte order, who knows) so I wasn't comfortable making the change myself.

Problem using image data in pillow

Using this code :

from PIL import Image as im
import numpy as np


file = pyexr.open("frame_0000.exr")
img = file.get("Image")
data = im.fromarray(img, 'RGBA')
data.show()

On this image :
source exr layer
I get this :
result png

here is the source file

I need the png image to be the same as the image from the exr.
Or in python extract a specific layer from an exr image.

Release 0.3.11 to PyPI

Hey tvogels! I was wondering if you could make a patch release to pypi containing the changes from the past few years? thanks!

type warnings from pylance for image returned from pyexr.read()

When I use Numpy methods on an image returned from pyexr.read() then Pylance gives me a warning.

E.g. VS Code gives me a red squiggle on .shape on the last line of this code:

import pyexr
impath = 'image.exr'
im = pyexr.read(impath)
im.shape

The full warning from Pylance is:

Cannot access member "shape" for type "Dict[str, ndarray[Unknown, Unknown]]"
  Member "shape" is unknownPylance[reportAttributeAccessIssue](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportAttributeAccessIssue)

I saw that you recently added type hints, however the issue seems to be that read() can return either an image or a dict.

A work around is to add a cast() wrapper:

from typing import cast
import pyexr
from numpy import ndarray

impath = 'image.exr'
im = cast(ndarray, pyexr.read(impath))
im.shape

This code gives no warnings from Pylance.

Instead of casting, adding an assertion also works:

assert isinstance(im, ndarray)

import error

OpenEXR.so: undefined symbol: _ZNK3Imf14TypedAttributeINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE12writeValueToERNS_7OStreamEi

Where the 'depth' variable comes from

Hi ,

Thanks for the great work,
while I'm trying to reproduce the WRITING part result as the readme file wrote, I have no idea where the 'depth' variable comes from, can you shade me a light.

Thanks

Building wheel for OpenEXR (setup.py) ... error

Hi, I got the below error message when I install OpenEXR on Windows 10. Is there any solution to this? Thanks a lot.

C:\Users\63040>pip install OpenEXR
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting OpenEXR
 Using cached https://pypi.tuna.tsinghua.edu.cn/packages/a6/b9/8cb5f0b2634d8e4a2fab2d188cd108011a676835f33b14c069c0d84cd494/OpenEXR-1.3.8.tar.gz (11 kB)
 Preparing metadata (setup.py) ... done
Building wheels for collected packages: OpenEXR
 Building wheel for OpenEXR (setup.py) ... error
 error: subprocess-exited-with-error

 × python setup.py bdist_wheel did not run successfully.
 │ exit code: 1
 ╰─> [10 lines of output]
     Looking for libOpenEXR...
     running bdist_wheel
     running build
     running build_py
     creating build
     creating build\lib.win-amd64-cpython-39
     copying Imath.py -> build\lib.win-amd64-cpython-39
     running build_ext
     building 'OpenEXR' extension
     error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
     [end of output]

 note: This error originates from a subprocess, and is likely not a problem with pip.
 ERROR: Failed building wheel for OpenEXR
 Running setup.py clean for OpenEXR
Failed to build OpenEXR
Installing collected packages: OpenEXR
 Running setup.py install for OpenEXR ... error
 error: subprocess-exited-with-error

 × Running setup.py install for OpenEXR did not run successfully.
 │ exit code: 1
 ╰─> [12 lines of output]
     Looking for libOpenEXR...
     running install
     D:\Code\lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
       warnings.warn(
     running build
     running build_py
     creating build
     creating build\lib.win-amd64-cpython-39
     copying Imath.py -> build\lib.win-amd64-cpython-39
     running build_ext
     building 'OpenEXR' extension
     error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/
     [end of output]

 note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

× Encountered error while trying to install package.
╰─> OpenEXR

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.