如何使用ImageIO的FreeImage将hdr转换为jpeg图像?
我已经编写了一个带EXR并将其转换为jpg的函数,结果如下:
所以我很难知道如何拍摄HDR并将其转换为具有类似结果的jpg。我从这里免费下载了HDR:https://hdrihaven.com/hdri/download.php?h=cayley_interior&r=1k
我只是不确定如何将hdr中的浮点数范围转换为jpg图像位深度。
HDR> JPG
import imageio
import os
def convert_hdr_to_jpg(filepath):
if not os.path.isfile(filepath):
return False
directory = os.path.dirname(filepath)
filename, extension = os.path.splitext(filepath)
if not extension.lower() in ['.hdr', '.hdri']:
return False
# imageio.plugins.freeimage.download() #DOWNLOAD IT
image = imageio.imread(filepath, format='HDR-FI')
output = os.path.join(directory, filename + '.jpg')
imageio.imwrite(output, image)
EXR> JPG
import os, json, logging, time, random
from math import sqrt
from collections import namedtuple
from PIL import Image
import numpy
import OpenEXR
import Imath
def convert_exr_to_jpg(filepath):
'''
Description:
Generates a jpg image for the supplied exr file
Args:
filepath (str): filepath to image
Returns:
bool: Returns True on success otherwise returns False
'''
if not os.path.isfile(filepath):
log.error('Missing file: {}'.format(filepath))
return False
name, extension = os.path.splitext(os.path.basename(filepath))
if extension not in ['.exr']:
log.warning('Invalid image file format: {}'.format(filepath))
return False
File = OpenEXR.InputFile(filepath)
PixType = Imath.PixelType(Imath.PixelType.FLOAT)
DW = File.header()['dataWindow']
Size = (DW.max.x - DW.min.x + 1, DW.max.y - DW.min.y + 1)
rgb = [numpy.fromstring(File.channel(c, PixType), dtype=numpy.float32) for c in 'RGB']
for i in range(3):
rgb[i] = numpy.where(rgb[i]<=0.0031308,
(rgb[i]*12.92)*255.0,
(1.055*(rgb[i]**(1.0/2.4))-0.055) * 255.0)
rgb8 = [Image.frombytes("F", Size, c.tostring()).convert("L") for c in rgb]
# rgb8 = [Image.fromarray(c.astype(int)) for c in rgb]
output = os.path.normpath(os.path.join(os.path.dirname(filepath), name + '.jpg'))
Image.merge("RGB", rgb8).save(output, quality=80)
log.info('Created: {}'.format(output))