# Image I/O and manipulation#

This how-to guide covers the visualization and manipulation of images with Bitmap. To get started, we import the mitsuba library and set a variant.

[1]:

import mitsuba as mi

mi.set_variant('scalar_rgb')


## Reading an image from disk#

Mitsuba provides a general-purpose class for reading, manipulating and writing images: Bitmap. Bitmap can load PNG, JPEG, BMP, TGA, as well as OpenEXR files, and it supports writing PNG, JPEG and OpenEXR files. For PNG and OpenEXR files, it can also write string-valued metadata, as well as the gamma setting.

Mitsuba makes it easy to load images from disk:

[2]:

bmp = mi.Bitmap('../scenes/textures/flower_photo.jpeg')


The string representation of the Bitmap class can be used to get more detailed information about the loaded image.

[3]:

print(bmp)

Bitmap[
pixel_format = rgb,
component_format = uint8,
size = [1500, 1500],
srgb_gamma = 1,
struct = Struct<3>[
uint8 R; // @0, normalized, gamma
uint8 G; // @1, normalized, gamma
uint8 B; // @2, normalized, gamma
],
data = [ 6.44 MiB of image data ]
]


Let’s break down those different pieces of information:

• Pixel format: Specifies the pixel format (e.g. RGBA or MultiChannel) that contains information about the number and order of channels.

• Size: Resolution of the image.

• Component format: Specifies how the per-pixel components are encoded (e.g. unsigned 8-bit integers or 32-bit floating point values).

• sRGB gamma correction: Indicates whether the gamma correction (sRGB ramp) is applied to the data.

• Internal structure: Describes the contents of the bitmap, its channels with names and format. For each channel, it also shows if premultiplied alpha is used.

By default, images loaded from PNG or JPEG will be treated as gamma corrected. This is not true for EXR images, so it is important to convert them with the convert() method if needed.

Different dedicated methods can be used to get and set various attributes, such as srgb_gamma() and set_srgb_gamma(). It is important to note that these methods won’t change the stored values. They only change how the Bitmap is interpreted later on.

For convenience, in Jupyter notebooks, the Bitmap type will automatically be displayed when used as the cell output as shown here:

[4]:

bmp

[4]: