{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Image I/O and manipulation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This how-to guide covers the visualization and manipulation of images with `Bitmap`.\n", "To get started, we import the `mitsuba` library and set a variant." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import mitsuba as mi\n", "\n", "mi.set_variant('scalar_rgb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Reading an image from disk\n", "\n", "Mitsuba provides a general-purpose class for reading, manipulating and writing images: [Bitmap][1]. `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.\n", "\n", "Mitsuba makes it easy to load images from disk:\n", "\n", "[1]: https://mitsuba.readthedocs.io/en/latest/src/api_reference.html#bitmap" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "bmp = mi.Bitmap('../scenes/textures/flower_photo.jpeg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The string representation of the Bitmap class can be used to get more detailed information about the loaded image." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bitmap[\n", " pixel_format = rgb,\n", " component_format = uint8,\n", " size = [1500, 1500],\n", " srgb_gamma = 1,\n", " struct = Struct<3>[\n", " uint8 R; // @0, normalized, gamma\n", " uint8 G; // @1, normalized, gamma\n", " uint8 B; // @2, normalized, gamma\n", " ],\n", " data = [ 6.44 MiB of image data ]\n", "]\n" ] } ], "source": [ "print(bmp)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's break down those different pieces of information:\n", "\n", "- **Pixel format**:\n", "Specifies the pixel format (e.g. `RGBA` or `MultiChannel`) that contains information about the number and order of channels.\n", "\n", "- **Size**:\n", "Resolution of the image.\n", "\n", "- **Component format**:\n", "Specifies how the per-pixel components are encoded (e.g. unsigned 8-bit integers or 32-bit floating point values). \n", "\n", "- **sRGB gamma correction**:\n", "Indicates whether the gamma correction (sRGB ramp) is applied to the data.\n", "\n", "- **Internal structure**:\n", "Describes the contents of the bitmap, its channels with names and format. For each channel, it also shows if [premultiplied alpha][1] is used.\n", "\n", " \n", "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()][2] method if needed.\n", "\n", "Different dedicated methods can be used to get and set various attributes, such as [srgb_gamma()][3] and [set_srgb_gamma()][4]. It is important to note that these methods won't change the stored values. They only change how the `Bitmap` is interpreted later on.\n", "\n", "For convenience, in Jupyter notebooks, the `Bitmap` type will automatically be displayed when used as the cell output as shown here:\n", "\n", "[1]: https://en.wikipedia.org/wiki/Alpha_compositing\n", "[2]: https://mitsuba.readthedocs.io/en/latest/src/api_reference.html#mitsuba.Bitmap.convert\n", "[3]: https://mitsuba.readthedocs.io/en/latest/src/api_reference.html#mitsuba.Bitmap.srgb_gamma\n", "[4]: https://mitsuba.readthedocs.io/en/latest/src/api_reference.html#mitsuba.Bitmap.set_srgb_gamma\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "