Created
May 30, 2019 17:21
-
-
Save taikomatsu/c39aaf55bf664b78ea8f83bb7c5ecad0 to your computer and use it in GitHub Desktop.
Convert HDRI to png
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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