Save a NumPy array as an image (Pillow, Matplotlib, OpenCV)
Codemia Knowledge Hub ·
The usual way to save a NumPy array as an image is Pillow: Image.fromarray(array).save("output.png"), with the array in uint8 (values 0 to 255). Matplotlib's plt.imsave accepts float arrays in the 0.0 to 1.0 range directly, and OpenCV's cv2.imwrite works too if you convert RGB to BGR first. Preparing the data correctly is what prevents corrupted or wrongly colored output.
Convert to uint8 first
Most image formats expect pixels as unsigned 8-bit integers (uint8) from 0 to 255. Floating-point arrays, common in scientific computing, need converting:
Skipping this is the most common mistake, often producing an all-black or garbled image. Libraries handle mismatched types differently: some silently produce garbled images, others throw errors. Also match the array shape to the format: (height, width, 3) for RGB, (height, width) for grayscale.
Pillow (PIL)
Pillow is the most widely used library for basic image I/O. Image.fromarray() turns an array into a PIL Image that you can save:
For grayscale, pass a 2D array and specify the mode, since leaving out mode='L' can misinterpret the data:
For RGBA (with transparency), use shape (height, width, 4):
Pillow requires uint8. A float array gives unexpected results or an error.
Matplotlib
plt.imsave is convenient if you already use Matplotlib, and it handles float arrays in the 0.0 to 1.0 range natively:
plt.imsave saves RGBA PNGs by default, even for grayscale input, so files may be larger than expected. It also clips float values outside 0.0 to 1.0, which can silently alter your data. Do not confuse its float range with uint8 arrays in the 0 to 255 range.
OpenCV
OpenCV is the standard for computer vision, and cv2.imwrite saves arrays to disk. The key difference is that OpenCV uses BGR channel order by default, not RGB:
Skip the conversion and red and blue are swapped in the saved image. Grayscale needs no conversion:
OpenCV can also save 16-bit images, useful for scientific and medical imaging:
Replacing the removed scipy.misc.imsave
Older tutorials use scipy.misc.imsave. It was deprecated in SciPy 1.0 and removed in SciPy 1.2, so replace it with Pillow:
Related reading
- Saving and Reading Bitmaps/Images from Internal memory in Android
- Saving image files in Tensorflow
- Saving the objects detected in a dataframe tensorflow object_detection
- Segmentation fault core dumped on tf.Session
- Saving interactive Matplotlib figures
- scaling inputs data to neural network
- Saving and reading variable size list from TFRecord
- Scala Wait while List is beeing filled
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.