JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Saving NumPy arrays as images in Python

Author:JIYIK Last Updated:2025/05/03 Views:

In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images.

An image can be thought of as an array of different pixels stored at specific locations with corresponding color codes. Hence, we may come across a situation where we need to convert and save an array as an image.

In this tutorial, we will discuss how to save a numpy array as an image.


Use Image.fromarray()the function to save a numpy array as an image

fromarray()The function is used to create an image memory from the object of the exported array. We can then save the image memory to our desired location by providing the desired path and file name.

For example,

import numpy as np
from PIL import Image

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

im = Image.fromarray(array)
im.save("filename.jpeg")

We first create an array to store the RGB color codes and then export it. We can specify the desired format of the image in the file name. It can be jpeg, pngor any other commonly used image format. This is common to all the methods discussed below.


Use imageio.imwrite()the function to save a numpy array as an image

Earlier, the scipy module had imsave()a function to save a numpy array as an image. However, in recent versions, it has been deprecated and the function image.io()in has been recommended imwrite()for this task and has gained popularity due to its simplicity.

The following code shows how to use this function.

import imageio
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

imageio.imwrite("filename.jpeg", array)

Use matplotlib.pyplot.imsave()the function to save a NumPy array as an image

The matplotlib module has a variety of functions for manipulating images.

imsave()The function can save the array as an image file.

For example,

import matplotlib.pyplot as plt
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

plt.imsave("filename.jpeg", array)

Use cv2.imwrite()the function to save a numpy array as an image

The OpenCV module is commonly used for image processing in Python. imwrite()The function in this module can export a numpy array to an image file.

For example,

import cv2
import numpy as np

array = np.arange(0, 737280, 1, np.uint8)
array = np.reshape(array, (1024, 720))

cv2.imwrite("filename.jpeg", array)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Convert Tensor to NumPy array in Python

Publish Date:2025/05/03 Views:85 Category:Python

This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra

Converting a PIL image to a NumPy array

Publish Date:2025/05/03 Views:148 Category:Python

This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi

How to Convert DataFrame Column to String in Pandas

Publish Date:2025/05/02 Views:162 Category:Python

We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a

How to count the frequency of values in a Pandas DataFrame

Publish Date:2025/05/02 Views:84 Category:Python

Sometimes, when you use DataFrame , you may want to count the number of times a value occurs in a column, or in other words, calculate the frequency. There are mainly three methods used for this. Let's look at them one by one. df.groupby().

How to get value from Pandas DataFrame cell

Publish Date:2025/05/02 Views:148 Category:Python

We'll look at using to get values ​​from cells in iloc Pandas , which is great for selecting by position, and how it differs from . We'll also learn about the and methods, which we can use when we don't want to set the return type to .

How to Add a Row to a Pandas DataFrame

Publish Date:2025/05/02 Views:127 Category:Python

Pandas is designed to load a fully populated DataFrame . We can pandas.DataFrame add them one by one in . This can be done by using various methods, such as .loc , dictionary, pandas.concat() or DataFrame.append() . .loc [index] Add rows to

How to change the order of Panas DataFrame columns

Publish Date:2025/05/02 Views:185 Category:Python

We will show how to use insert and reindex to change the order of columns in different ways pandas.DataFrame , such as assigning column names in a desired order. pandas.DataFrame Sort the columns in the new order The easiest way is columns

How to pretty print an entire Pandas Series/DataFrame

Publish Date:2025/05/02 Views:168 Category:Python

We will introduce various methods to pretty print the entire Pandas Series/DataFrame, such as option_context, set_option, and options.display. option_context Pretty Printing Pandas DataFrame We can option_context use with one or more option

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial