python - 使用 ImageIO 和 Python 将 EXR 转换为 JPEG
问题描述
我只是想将 EXR 转换为 jpg 图像,但结果却很暗。有谁知道我在这里做错了什么?我正在标准化图像值,然后将它们放入 0-255 色彩空间。它仍然看起来不正确。
用于测试 exr 图像的 Dropbox 链接:https ://www.dropbox.com/s/9a5z6fjsyth7w98/torus.exr?dl=0
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 位深度(通道)的 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)
这是带有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)
(使用 python 3.5.2、Ubuntu 16.04 测试)
推荐阅读
- javascript - 如何使用javascript从特定的css类中获取价值
- python - 我正在抓取以下包含 java 脚本但出现错误的网站
- migration - MIP - 从 AIP 迁移
- css - Wordpress 仅通过编辑器在帖子中不显示 sup/sub
- macos - macOS 中的主文件夹被点文件弄得杂乱无章
- php - 基本的 PHP 时间计算有错误的答案
- sql - SQL:当某些月份没有记录时,如何查询每月总和的平均值?
- python - Selenium Error Load Cookies 只能为当前域设置
- python - 在 python 中,如何处理许多在 numpy 或列表中不按顺序排列的矩形?
- java - OpenStack 项目列表 HTTP 请求忽略分页 "limit=" 参数