How can I use ImageIO's FreeImage to convert and hdr to a jpeg image?
I've written a function that takes and EXR and converts it to a jpg and the results looks like this:
So I'm stumped on how to take an HDR and convert it to a jpg with similar results. I downloaded the HDR from here for free: https://hdrihaven.com/hdri/download.php?h=cayley_interior&r=1k
I'm just not sure how to go about converting the floating number range in the hdr to a jpg image bit-depth.
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))