首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用FreeImage将hdr转换为jpg

使用FreeImage将hdr转换为jpg
EN

Stack Overflow用户
提问于 2017-12-09 03:53:49
回答 1查看 1.4K关注 0票数 1

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

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

因此,我很难理解如何将HDR转换成具有类似结果的jpg。我从这里免费下载了HDR:interior&r=1k

我只是不知道如何将hdr中的浮动数字范围转换为jpg图像位深度。

人类发展报告> JPG

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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))
EN

回答 1

Stack Overflow用户

发布于 2022-11-10 08:23:24

代码语言:javascript
运行
复制
sudo apt-get install -y openimageio-tools
oiiotool probe.hdr -o probe.png
# https://github.com/OpenImageIO/oiio/discussions/3579
# https://launchpad.net/ubuntu/+source/openimageio
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47725161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档