Image VisualizationΒΆ
It is often useful to incorporate bitmap images into a visualization. To
facilitate this, Toyplot provides toyplot.canvas.Canvas.image()
and toyplot.image()
functions, which can display image data
supplied in a couple of different formats. Note that as a visualization
library Toyplot does not provide functionality for reading, writing,
or manipulating images - you should do that with libraries such as
PIL
or scikit-image
instead.
For example, you can pass any PIL.Image.Image
directly to
Toyplot:
import PIL.Image
import toyplot
image = PIL.Image.open("../artwork/toyplot.png")
canvas, mark = toyplot.image(image, width=300)
As always, the width
parameter in the above example is the width of
the Toyplot canvas. The image is then scaled uniformly to fit. Of
course, you can use any Canvas Layout method you like to
combine images and marks together on the canvas:
import numpy
canvas = toyplot.Canvas(width=600, height=300)
mark = canvas.image(image, grid=(1,2,0))
axes = canvas.cartesian(grid=(1,2,1))
axes.plot(numpy.linspace(0, 1) ** 2)
canvas.text(150, 250, "( Toyplot Logo )", style={"font-size":"14px"});
In addition to PIL images, Toyplot can display any scikit-images
compatible image, i.e. any numpy array with appropriate shape and data
type. For example, the following sample data provided with scikit-images
is a \(512 \times 512\) image containing three-channel 8-bit RGB
color:
import skimage.data
image = skimage.data.astronaut()
image.shape, image.dtype
((512, 512, 3), dtype('uint8'))
canvas, mark = toyplot.image(image, width=300)
This is especially useful when working with scikit-images in a Jupyter notebook.
There are many combinations of channel, data type, and bit depth than can be displayed. The following example converts the data to a single channel containing floating-point grayscale color:
import skimage.color
gray = skimage.color.rgb2gray(image)
gray.shape, gray.dtype
((512, 512), dtype('float64'))
canvas, mark = toyplot.image(gray, width=300)
You can even display one-bit images:
import skimage.filters
binary = gray > skimage.filters.threshold_isodata(gray)
binary.shape, binary.dtype
((512, 512), dtype('bool'))
canvas, mark = toyplot.image(binary, width=300)
Grayscale data can also be mapped using Toyplotβs usual Color Mapping functionality:
colormap = toyplot.color.linear.map("Blackbody")
canvas, mark = toyplot.image((gray, colormap), width=300)