Array2image
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_rgbfunction 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.
Installation
pip install array2image
Requires python 3.10+.
Documentation
Function signature
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
Argument description
- 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_sizeif 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.
Examples
1-channel arrays
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)
| Random | MNIST | |
from array2image import array_to_image
# Represent only a 4D array
image = array_to_image(array)
|
|
|
from array2image import array_to_image
# Force 0 pixel for all grid levels
image = array_to_image(
array,
grid_thickness=(0, 0)
)
|
|
|
from array2image import array_to_image
# Invert colors
image = array_to_image(
array,
inverted_colors=True
)
|
|
|
from array2image import array_to_image
import matplotlib
# Use an external colormap
image = array_to_image(
array,
cmap=matplotlib.cm.viridis
)
|
|
|
from array2image import array_to_image
import matplotlib
# Represent only a 2D array
image = array_to_image(
array[0, 0],
cmap=matplotlib.cm.viridis
)
|
|
|
from array2image import array_to_image
import matplotlib
# Show a grid
image = array_to_image(
array[0, 0],
cmap=matplotlib.cm.viridis,
grid_thickness=1
)
|
|
|
from array2image import array_to_image
# Fix the bin size
image = array_to_image(
array[0, 0],
bin_size=4
)
|
|
|
from array2image import array_to_image
# Fix a specific asymetric bin size
image = array_to_image(
array[0, 0],
bin_size=(4,8)
)
|
|
|
2-channel arrays
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)
| Random | Fourier | |
from array2image import array_to_image
# Default Hue/Saturation colormap
image = array_to_image(array)
|
|
|
from array2image import array_to_image
import colormap2d
# External 2D colormap
array_to_image(
array,
cmap=colormap2d.pinwheel
)
|
|
|
3-channel arrays
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)
| Random | Lena | |
from array2image import array_to_image
# Default RGB colormap
image = array_to_image(array)
|
|
|
from array2image import array_to_image
import matplotlib
# External 3D colormap
array_to_image(
array,
cmap=matplotlib.colors.hsv_to_rgb
)
|
|
|
Metadata
Release files for array2image 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| array2image-0.1.0.tar.gz | 10.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| array2image-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 20.8 kB
Release files / array2image-0.1.0.tar.gz
| Download URL | array2image-0.1.0.tar.gz |
|---|---|
| Size | 10.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7bf1b2859bdedb0c64b0e888676a99369603f13bf1378f72ace029660e31df5d
|
|
BLAKE2b-256 checksum How to use checksums |
846228546b060ed4824dcea3868df6b171d3319b727de7be7b00d6a433228151
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.1 CPython/3.11.6
|
Release files / array2image-0.1.0-py3-none-any.whl
| Download URL | array2image-0.1.0-py3-none-any.whl |
|---|---|
| Size | 10.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
664f43f0dcd393e0df845254bc0f7c1b8e495058f36048c33f4248da13603830
|
|
BLAKE2b-256 checksum How to use checksums |
c0039fdd2bf5d60ab7f7adbbe6ed88ca642bca5b29f8146a59f82127c30a87f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.10.1 CPython/3.11.6
|