1

如何使用 ImageIO 的 FreeImage 将 hdr 转换为 jpeg 图像?

我编写了一个函数,它接受和 EXR 并将其转换为 jpg,结果如下所示:

在此处输入图像描述

因此,我对如何拍摄 HDR 并将其转换为具有相似结果的 jpg 感到困惑。我从这里免费下载了 HDR:https ://hdrihaven.com/hdri/download.php?h=cayley_interior&r=1k

我只是不确定如何将 hdr 中的浮点数范围转换为 jpg 图像位深度。

高动态范围 > 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))
4

0 回答 0