使用ImageIO和Python将EXR转换为JPEG

时间:2018-06-07 18:37:06

标签: python python-imageio

我只是想将转换和EXR转换为jpg图像,但我的结果却变得很暗。有谁知道我在这里做错了什么?我将图像值标准化,然后将它们放入0-255色空间。但它看起来仍然不正确。

用于测试exr图片的Dropbox链接:https://www.dropbox.com/s/9a5z6fjsyth7w98/torus.exr?dl=0

enter image description here

import sys, os
import imageio

def convert_exr_to_jpg(exr_file, jpg_file):
    if not os.path.isfile(exr_file):
        return False

    filename, extension = os.path.splitext(exr_file)
    if not extension.lower().endswith('.exr'):
        return False

    # imageio.plugins.freeimage.download() #DOWNLOAD IT
    image = imageio.imread(exr_file, format='EXR-FI')

    # remove alpha channel for jpg conversion
    image = image[:,:,:3]

    # normalize the image
    data = image.astype(image.dtype) / image.max() # normalize the data to 0 - 1
    data = 255 * data # Now scale by 255
    rgb_image = data.astype('uint8')
    # rgb_image = imageio.core.image_as_uint(rgb_image, bitdepth=8)

    imageio.imwrite(jpg_file, rgb_image, format='jpeg')
    return True


if __name__ == '__main__':
    exr = "C:/Users/John/images/torus.exr"
    jpg = "C:/Users/John/images/torus.jpg"
    convert_exr_to_jpg(exr, jpg)

2 个答案:

答案 0 :(得分:0)

示例图像是具有 16位深度(通道)的EXR图像。这是一个Python脚本,可通过 opencv 将exr图像转换为 png

import numpy as np
import cv2
im=cv2.imread("torus.exr",-1)
im=im*65535
im[im>65535]=65535
im=np.uint16(im)
cv2.imwrite("torus.png",im)

enter image description here

这是带有 imageio 的修改后的代码,该代码将以 jpeg 格式保存图像

import sys, os
import imageio

def convert_exr_to_jpg(exr_file, jpg_file):
    if not os.path.isfile(exr_file):
        return False

    filename, extension = os.path.splitext(exr_file)
    if not extension.lower().endswith('.exr'):
        return False

    # imageio.plugins.freeimage.download() #DOWNLOAD IT
    image = imageio.imread(exr_file)
    print(image.dtype)

    # remove alpha channel for jpg conversion
    image = image[:,:,:3]


    data = 65535 * image
    data[data>65535]=65535
    rgb_image = data.astype('uint16')
    print(rgb_image.dtype)
    #rgb_image = imageio.core.image_as_uint(rgb_image, bitdepth=16)

    imageio.imwrite(jpg_file, rgb_image, format='jpeg')
    return True


if __name__ == '__main__':
    exr = "torus.exr"
    jpg = "torus3.jpeg"
    convert_exr_to_jpg(exr, jpg)

enter image description here

(使用python 3.5.2,Ubuntu 16.04测试)

答案 1 :(得分:0)

我遇到了同样的问题,并在这里解决。由于ImageIO会将所有内容都转换为numpy数组,因此您可以伽马校正值(解决您的黑暗问题),然后将其转换回PIL图像以轻松使用:

im = imageio.imread("image.exr")
im_gamma_correct = numpy.clip(numpy.power(im, 0.45), 0, 1)
im_fixed = Image.fromarray(numpy.uint8(im_gamma_correct*255))

我在您非常漂亮的圆环结上进行了测试,效果很好。请告诉我您是否需要更完整的代码段,但我认为以上内容可以回答您的实际问题。