Can you write a function like that but using different image making lib? Should work in colab and automatically making pixels bigger if it's e.g. 30 x 30 input array
Below is a drop-in replacement that uses Pillow (PIL) instead of Matplotlib.
It automatically “zooms‐in” small arrays (e.g. 30 × 30) so every numerical pixel
is shown as a chunky, visible square.
It works out-of-the-box in Google Colab, Jupyter, or a regular Python script.
python
# ---------------------------------------------------------------
# draw_pil.py
# A tiny helper that visualises a 2-D NumPy array via Pillow
# ---------------------------------------------------------------
import numpy as np
from PIL import Image
from matplotlib import cm # only for the colour-map
from IPython.display import display # nice inline display in Colab / Jupyter
def draw(arr, cmap: str = 'copper', max_canvas_px: int = 512, show: bool = True):
"""
Visualise a 2-D NumPy array using Pillow instead of Matplotlib.
Parameters
----------
arr : np.ndarray
A 2-D (H×W) NumPy array (float or int).
cmap : str, default 'copper'
Any Matplotlib-compatible colormap name.
max_canvas_px : int, default 512
Maximum size (in pixels) for the longest image edge.
Arrays smaller than this will be up-scaled with *nearest* resampling
so individual elements look like fat pixels.
show : bool, default True
If True, the image is displayed inline (Colab/Jupyter).
The `PIL.Image` object is always returned.
Returns
-------
PIL.Image.Image
The generated RGB image (can be saved via `img.save('foo.png')`).
"""
# 1) Normalise the data to 0‒1
arr = np.asarray(arr)
data_min, data_max = float(arr.min()), float(arr.max())
norm = (arr - data_min) / (data_max - data_min + 1e-12)
# 2) Apply the chosen Matplotlib colormap → RGBA (0‒1 floats)
rgba_float = cm.get_cmap(cmap)(norm)
# 3) Discard the alpha channel & convert to 8-bit uint
rgb_uint8 = (rgba_float[..., :3] * 255).astype(np.uint8)
# 4) Build a Pillow image
im = Image.fromarray(rgb_uint8, mode='RGB')
# 5) Auto-scale small images (nearest-neighbour so it stays pixel-art-y)
h, w = arr.shape
scale_factor = max(1, max_canvas_px // max(h, w))
if scale_factor > 1:
im = im.resize((w * scale_factor, h * scale_factor), Image.NEAREST)
# 6) Display inline if requested
if show:
display(im)
return im
How the auto-scaling works
• For arrays that already exceed max_canvas_px in either dimension
→ no scaling (your big arrays stay at 1-pixel-per-element).
• For smaller arrays (e.g. 30 × 30)
→ scale = ⌊max_canvas_px / max(H, W)⌋, so 30 × 30 becomes 17× larger
(30 × 17 ≈ 510 px).
Quick demo (run this in Colab)
python
import numpy as np
from draw_pil import draw # or paste the function directly
# Example 1 – small 30×30 heat map
small = np.random.rand(30, 30)
draw(small) # chunky visible pixels
# Example 2 – larger 256×256 gradient
x = np.linspace(0, 1, 256)
grad = np.outer(x, x[::-1])
draw(grad, cmap='viridis') # no up-scaling, fits nicely
# Saving to disk if needed
img = draw(grad, show=False) # suppress inline
img.save('gradient.png')
Installation notes
Google Colab already ships with Pillow and Matplotlib.
If you’re in a fresh environment run once: