gpt4 book ai didi

python - 使用 ImageIO 和 Python 将 EXR 转换为 JPEG

转载 作者:行者123 更新时间:2023-11-28 19:02:45 27 4
gpt4 key购买 nike

我只是想将 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)

最佳答案

示例图像是具有 16 位深度( channel )的 EXR 图像。这是一个使用 opencv 将 exr 图像转换为 png 的 python 脚本。

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 测试)

关于python - 使用 ImageIO 和 Python 将 EXR 转换为 JPEG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50748084/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com