In [1]:
import os
import numpy as np
import mitsuba

# Set the desired mitsuba variant
mitsuba.set_variant('scalar_rgb')

from mitsuba.core import Bitmap, Struct, Thread
from mitsuba.core.xml import load_file

%matplotlib notebook
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['savefig.dpi'] = 80
mpl.rcParams['figure.dpi'] = 80
In [2]:
# Absolute or relative path to the XML file
filename = 'cbox-rgb.xml'

# Add the scene directory to the FileResolver's search path
Thread.thread().file_resolver().append(os.path.dirname(filename))

# Load the actual scene
scene = load_file(filename)

# Call the scene's integrator to render the loaded scene
scene.integrator().render(scene, scene.sensors()[0])

# After rendering, the rendered data is stored in the film
film = scene.sensors()[0].film()

# Write out rendering as high dynamic range OpenEXR file
film.set_destination_file('output.exr')
film.develop()
2020-04-10 21:22:16 INFO main [xml.cpp:1129] Loading XML file "cbox-rgb.xml" ..
2020-04-10 21:22:16 INFO main [xml.cpp:1130] Using variant "scalar_rgb"
2020-04-10 21:22:16 INFO main [xml.cpp:616] Loading included XML file "/mnt/hdd/demo/git/mitsuba-data/scenes/cbox/fragments/base.xml" ..
2020-04-10 21:22:16 INFO main [xml.cpp:616] Loading included XML file "/mnt/hdd/demo/git/mitsuba-data/scenes/cbox/fragments/bsdfs-rgb.xml" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/srgb.so" ..
2020-04-10 21:22:16 INFO main [xml.cpp:616] Loading included XML file "/mnt/hdd/demo/git/mitsuba-data/scenes/cbox/fragments/shapes.xml" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/srgb_d65.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/path.so" ..
2020-04-10 21:22:16 INFO tbb00 [PluginManager] Loading plugin "plugins/diffuse.so" ..
2020-04-10 21:22:16 INFO tbb02 [PluginManager] Loading plugin "plugins/obj.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/independent.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/box.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/hdrfilm.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/perspective.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/area.so" ..
2020-04-10 21:22:16 INFO main [PluginManager] Loading plugin "plugins/uniform.so" ..
2020-04-10 21:22:16 INFO main [ShapeKDTree] Building a SAH kd-tree (38 primitives) ..
2020-04-10 21:22:16 INFO main [ShapeKDTree] Finished. (1.53 KiB of storage, took 0ms)
2020-04-10 21:22:16 INFO main [SamplingIntegrator] Starting render job (256x256, 256 samples, 6 threads)
2020-04-10 21:22:24 INFO main [SamplingIntegrator] Rendering finished. (took 8.093s)
2020-04-10 21:22:24 INFO main [HDRFilm] ✔ Developing "output.exr" ..
In [3]:
# Write out a tonemapped JPG of the same rendering
bmp = film.bitmap(raw=True)
bmp.convert(Bitmap.PixelFormat.RGB, Struct.Type.UInt8, srgb_gamma=True).write('output.jpg')

# Get linear pixel values as a numpy array for further processing
bmp_linear_rgb = bmp.convert(Bitmap.PixelFormat.RGB, Struct.Type.Float32, srgb_gamma=False)
image_np_f = np.array(bmp_linear_rgb)

bmp_linear_rgb = bmp.convert(Bitmap.PixelFormat.RGB, Struct.Type.Float32, srgb_gamma=True)
image_np_t = np.array(bmp_linear_rgb)

print(image_np_f.shape)
(256, 256, 3)
In [6]:
# 結果を表示
plt.figure(figsize=(10, 5))
plt.subplot(1, 2, 1)
plt.imshow(image_np_f)
plt.title("srgb_gamma=False")
plt.grid("off")
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(image_np_t)
plt.title("srgb_gamma=True")
plt.grid("off")
plt.axis("off")
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Out[6]:
(-0.5, 255.5, 255.5, -0.5)
In [ ]: