Incorrect Color Space Conversion When Exporting Images from OpenEXR to PNG
When converting images from OpenEXR to PNG using the ExportImage class, the resulting PNG images appear darker than the original OpenEXR images. This issue seems to stem from a discrepancy in the color spaces used by the two image formats. OpenEXR images use a linear color space, while PNG images use a gamma-corrected color space.
The current implementation of the ExportImage class does not appear to account for this difference in color spaces. When creating a new image with bpy.data.images.new and setting its pixels with tmp_image.pixels.foreach_set(pixels), the pixel data is assumed to be in the correct color space for the image format. However, if the pixel data is in a linear color space (as it would be for an OpenEXR image), and a PNG image is being created (which uses a gamma-corrected color space), the resulting image will appear darker than expected.
To resolve this issue, a gamma correction should be applied to the pixel data before setting it on the new image. This can be achieved with the following code:
gamma = 2.2
pixels = np.power(pixels, 1 / gamma)
This code raises each pixel value to the power of 1 / gamma, effectively applying a gamma correction to the pixel data. The value of gamma is typically 2.2 for sRGB images, which is the color space used by PNG images.
Please note that this is solution is specific to typical PNG color space and the actual process of color space conversion may be more complex than my expertise, depending on the specific color profiles of the images involved. You may need to implement a more comprehensive color space conversion process to ensure accurate results for all image conversions. However, blender has some built in modules for saving images in new formats from raw pixel data. You may be able to leverage that.
Expected result:
The PNG image should have the same brightness and color as the original OpenEXR image as shown below.
Actual result:
The PNG image appears darker than the original OpenEXR image.
@tdw46 it appears that you are reporting a bug in Blender? If so, it would be best to file an issue at https://github.com/KhronosGroup/glTF-Blender-IO/issues.