 # Convert a NumPy array to an image

## Convert a NumPy array to an image

NumPy Or numeric python is a popular library for array manipulation. Since images are just an array of pixels carrying various color codes. NumPy can be used to convert an array into image. Apart from NumPy we will be using PIL or Python Image Library also known as Pillow to manipulate and save arrays.

Approach:

1. Create a numpy array.
2. Reshape the above array to suitable dimensions.
3. Create an image object from the above array using PIL library.
4. Save the image object in a suitable file format.

Below is the implementation:

 `# Python program to convert` `# numpy array to image` ` ` `# import required libraries` `import` `numpy as np` `from` `PIL ``import` `Image as im` ` ` `# define a main function` `def` `main():` ` ` `    ``# create a numpy array from scratch` `    ``# using arange function.` `    ``# 1024x720 = 737280 is the amount ` `    ``# of pixels.` `    ``# np.uint8 is a data type containing` `    ``# numbers ranging from 0 to 255 ` `    ``# and no non-negative integers` `    ``array ``=` `np.arange(``0``, ``737280``, ``1``, np.uint8)` `     ` `    ``# check type of array` `    ``print``(``type``(array))` `     ` `    ``# our array will be of width ` `    ``# 737280 pixels That means it ` `    ``# will be a long dark line` `    ``print``(array.shape)` `     ` `    ``# Reshape the array into a ` `    ``# familiar resoluition` `    ``array ``=` `np.reshape(array, (``1024``, ``720``))` `     ` `    ``# show the shape of the array` `    ``print``(array.shape)` ` ` `    ``# show the array` `    ``print``(array)` `     ` `    ``# creating image object of` `    ``# above array` `    ``data ``=` `im.fromarray(array)` `     ` `    ``# saving the final output ` `    ``# as a PNG file` `    ``data.save(``'gfg_dummy_pic.png'``)` ` ` `# driver code` `if` `__name__ ``=``=` `"__main__"``:` `   ` `  ``# function call` `  ``main()`

Output:

```<class 'numpy.ndarray'>
(737280,)
(1024, 720)
[[  0   1   2 ... 205 206 207]
[208 209 210 ... 157 158 159]
[160 161 162 ... 109 110 111]
...
[144 145 146 ...  93  94  95]
[ 96  97  98 ...  45  46  47]
[ 48  49  50 ... 253 254 255]]
``` gfg_dummy_pic.png 1024 x 720

Note: Every array can’t be converted into an image because each pixel of an image consist of specific color code and if the given array is not in a suitable format the libraries won’t be able to process it Properly.

Last Updated on November 11, 2021 by admin

## Pandas dataframe.assign()Pandas dataframe.assign()

Pandas dataframe.assign()   Dataframe.assign() method assign new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original ones. Existing columns that are re-assigned will be overwritten. Length of newly assigned column must

## How to Run a Python Script using Docker?How to Run a Python Script using Docker?

How to Run a Python Script using Docker? Python is one of the most popular and widely used programming languages. Right from building a simple GUI app using Tkinter to building a full-fledged web application, Python is used everywhere. In

## Python | Pandas dataframe.shift()Python | Pandas dataframe.shift()

Pandas dataframe.shift() function Shift index by desired number of periods with an optional time freq. This function takes a scalar parameter called period, which represents the number of shifts to be made over the desired axis. This function is very

## How to Get a List of Class Attributes in Python?How to Get a List of Class Attributes in Python?

How to Get a List of Class Attributes in Python? A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new

## Convert Excel to CSV in PythonConvert Excel to CSV in Python

Convert Excel to CSV in Python   In this article, we will be dealing with the conversion of Excel (.xlsx) file into .csv.  There are two formats mostly used in Excel : (*.xlsx) : Excel Microsoft Office Open XML Format

## Python format() functionPython format() function

In Python, the format() function is a powerful tool that simplifies string formatting. It allows you to create formatted strings by replacing placeholders with specific values. This article will guide you through the various features and techniques of using the

## Python PIL getpixel() MethodPython PIL getpixel() Method

Python PIL | getpixel() Method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The PixelAccess class provides read and write access to PIL.Image data at a pixel level. Accessing individual pixels is fairly slow. If

## Python – Simple GUI calculator using TkinterPython – Simple GUI calculator using Tkinter

Python | Simple GUI calculator using Tkinter Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk