How to convert a NumPy array to PIL image applying matplotlib colormap
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting numeric arrays into images is common in machine learning, geospatial analysis, and scientific dashboards. A raw NumPy array usually stores scalar values, not display-ready colors, so a colormap step is what makes the data interpretable. This guide shows a repeatable path from a two-dimensional NumPy array to a PIL image using Matplotlib.
Build a Safe Array-to-Image Pipeline
A reliable conversion has four explicit stages: normalize values, apply a colormap, scale to uint8, and create a PIL image. Keeping each stage explicit prevents accidental dtype or range bugs that produce washed-out or overly dark output.
The script below is runnable as-is. It generates synthetic data, applies viridis, and writes a PNG.
If your array already has a stable physical range, use that known range for normalization instead of per-image min and max. This keeps colors comparable across multiple frames or experiments.
Handle Outliers and Preserve Detail
Many datasets have a small number of extreme values. If you normalize directly with global min and max, those extremes can compress useful mid-range information into a tiny color band. A good fix is percentile clipping before normalization.
This function is practical in production because it centralizes policy decisions such as clipping and output mode. That consistency is useful for dashboards and model diagnostics.
Move Between PIL, NumPy, and Other Libraries
After conversion, PIL gives you simple save and resize operations, while NumPy remains ideal for numeric transforms. You can convert back with np.array(image) whenever downstream code expects an array. If you pass data into OpenCV, remember OpenCV defaults to BGR channel order, so convert deliberately to avoid color shifts.
When building reports, generate both raw grayscale and colorized outputs. Grayscale keeps numeric intuition for technical readers, while colormaps improve quick visual scanning for non-technical stakeholders.
Common Pitfalls
- Skipping normalization and expecting different arrays to map to comparable colors.
- Passing a three-dimensional array into a scalar colormap flow that expects height by width data.
- Forgetting to cast to
uint8before creating an RGB PIL image. - Keeping the alpha channel by accident when the downstream pipeline expects plain RGB.
- Normalizing each frame independently in a time series, which makes color meaning drift over time.
Summary
- Use an explicit pipeline: normalize, map colors, scale to
uint8, then create a PIL image. - Apply percentile clipping when outliers hide important structure.
- Keep output mode intentional, especially when moving among PIL, NumPy, and OpenCV.
- Use one reusable helper to keep image generation behavior stable across projects.
- Validate array shape and dtype early to avoid hard-to-debug rendering artifacts.

