Array2image helps you convert Numpy arrays to PIL images. It comes with a single function array_to_image()
.
When given an array, it automatically guesses its spatial and channel dimensions. Spatial dimensions greater than 2 are considered as images of images. The resulting image is then represented differently depending on the channel dimension:
- 1D channel: greyscale image.
- 2D channel: image with varying hue and saturation.
- 3D channel: RGB image.
If specified, custom colormap functions can be used instead. For instance:
matplotlib.cm.*
functions for 1D channel arrays (likematplotlib.cm.viridis
)colormap2d.*
functions for 2D channel arrays (likecolormap2d.pinwheel
)- The
matplotlib.colors.hsv_to_rgb
function for 3D channel arrays.`
It assumes that values are floats between 0 and 1 or integers between 0 and 255 (values are clipped anyway). If specified, it automatically normalizes the values.
Why not directly use matplotlib.plt.imshow
instead? If you have 2D array with 1 or 3-channel data and don't care about the size nor the incrusted axis in the returned image, matplotlib.plt.imshow
is great. The Array2image library makes the focus on simplicity by guessing an appropriate way of rendering non-generic arrays.
pip install array2image
Requires python 3.10+.
def array_to_image(
arr,
spatial_dims: tuple[int, ...] | None = None,
channel_dim: int | None = None,
cmap: Callable | None = None,
inverted_colors: bool = False,
bin_size: int | tuple[int, int] | None = None,
target_total_size: int = 200,
grid_thickness: int | tuple[int, ...] = 0,
norm: bool = False,
) -> PIL.Image
- arr: Array-like to be converted.
- spatial_dims: Spatial dimensions of the array. If None, spatial dimensions are automatically guessed.
- channel_dim: Channel dimension of the array. Only 1, 2 or 3 channel dimension arrays can be converted to an image. If None, the channel dimension is automatically guessed.
- cmap: Colormap function to be used if provided. If None, default built-in functions are used.
- inverted_colors: If True, inverts the color of the image.
- bin_size: Number of pixels for each array spatial element.
target_total_size: Target size of the image. Used to automatically choose
bin_size
if the latter is None. - grid_thickness: Tuple of grid thickness for each level of 2D spatial dimensions. By default, it is 0 for the last 2D dimensions and 2 pixels for the others.
- norm: If True, normalize values between 0 and 1 with a min-max normalization.
Data for the following examples:
import numpy as np
# Random data: A 2x4x10x8 Numpy array with random values between 0 and 1
np.random.seed(0)
array = np.random.uniform(0, 1, (2, 4, 10, 8))
# MNIST data: The first 48 MNIST digits organized in a 6x8 grid.
mnist_data = ...
array = mnist_data[:48].reshape(6, 8, 28, 28)
Data for the following examples:
import numpy as np
# Random data: A 10x10x2 Numpy array with random values between 0 and 1
np.random.seed(0)
array = np.random.uniform(0, 1, (10, 10, 2))
# Dummy fourier data: linearly varying phase and magnitude over a 2D grid
phase, amplitude = np.meshgrid(np.linspace(0,1,10), np.meshgrid(np.linspace(0,1,10)))
array = np.stack((phase, amplitude), axis=-1)
Data for the following examples:
import numpy as np
# Random data: A 10x10x3 Numpy array with random values between 0 and 1
np.random.seed(0)
array = np.random.uniform(0, 1, (10, 10, 3))
# The Lena RGB image
image = Image.open("lena.png")
array = np.asarray(image)