Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert HDRI to png
import os
import os.path
import imageio
import numpy as np
from PIL import Image
# .hdrや.exrなど、読み込む際にfreeimageを使う必要があるフォーマット用にfreeimageをダウンロードしてくれるらしい
imageio.plugins.freeimage.download()
def list_hdri(dirname):
exts = ['.hdr', '.exr']
hdris = ['{}/{}'.format(dirname, o) for o in os.listdir(dirname) if os.path.splitext(o)[-1] in exts]
return hdris
# ディレクトリ内のHDRI(.hdr, .exr)を取得
img_dir = 'C:/Users/user/lib/HDRI'
imgpaths = list_hdri(img_dir)
# 画像サイズの設定
w = 600
print('[INFO] {} files found.'.format(len(imgpaths)))
for i, imgpath in enumerate(imgpaths):
print(i+1, imgpath)
# imageioを使って画像の読み込み
img = imageio.imread(imgpath)
# 簡単なガンマ補正
img = np.power(img, 1/2.2)
# float32をuint8に変換
img = np.clip(np.floor(img*255), 0, 255).astype(np.uint8)
# numpy.ndarrayをPIL.Imageに変換
image = Image.fromarray(img)
# 大きい方をwのサイズに合わせる感じで雑にリサイズ
image.thumbnail((w, w), Image.ANTIALIAS)
# ファイルネームの拡張子を変えて保存
basename, ext = os.path.splitext(os.path.normpath(imgpath))
newfile = '{}.png'.format(basename)
image.save(newfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.