Numpy Tutorial

Creating NumPy Array

NumPy Array Manipulation

Matrix in NumPy

Operations on NumPy Array

Reshaping NumPy Array

Indexing NumPy Array

Arithmetic operations on NumPy Array

Linear Algebra in NumPy Array

NumPy and Random Data

Sorting and Searching in NumPy Array

Universal Functions

Working With Images

Projects and Applications with NumPy

Convert a NumPy array to an image

Converting a NumPy array to an image is a common operation when working with image data. This tutorial will show you how to accomplish this using the PIL (Python Imaging Library) module from the Pillow package.

Convert a NumPy Array to an Image using Pillow

1. Setup:

If you haven't already installed the Pillow library, do so with the following command:

pip install Pillow

Now, import the necessary libraries:

import numpy as np
from PIL import Image

2. Creating an Example NumPy Array:

For this tutorial, let's create a simple grayscale image (256x256) where pixel values range from 0 (black) to 255 (white):

# Create a gradient image
arr = np.linspace(0, 255, 256*256).reshape(256, 256).astype(np.uint8)

3. Convert the NumPy Array to an Image:

image = Image.fromarray(arr, 'L')  # 'L' indicates the mode is grayscale

4. Save and Display the Image:

Now, you can save the image to a file:

image.save('gradient_image.png')

To display the image directly from your script:

image.show()

5. Handling Color Images:

For color images, the NumPy array would have a shape (height, width, 3) for RGB images or (height, width, 4) for RGBA images.

Here's a simple example of an RGB image where channels are divided into red, green, and blue:

red = np.array([[[255, 0, 0]]*256]*256, dtype=np.uint8)
green = np.array([[[0, 255, 0]]*256]*256, dtype=np.uint8)
blue = np.array([[[0, 0, 255]]*256]*256, dtype=np.uint8)

image_red = Image.fromarray(red)
image_green = Image.fromarray(green)
image_blue = Image.fromarray(blue)

image_red.save('red_image.png')
image_green.save('green_image.png')
image_blue.save('blue_image.png')

6. Conclusion:

Using the Pillow library, it's straightforward to convert NumPy arrays into images. This operation is beneficial when you need to visualize data from your arrays or when working with image processing tasks in Python. Remember to ensure your array data types and shapes are suitable for the kind of image you're trying to produce.

1. Convert NumPy array to image in Python:

Converting a NumPy array to an image using matplotlib.pyplot.imshow().

import numpy as np
import matplotlib.pyplot as plt

# Creating a NumPy array
image_array = np.random.random((100, 100))

# Displaying the image
plt.imshow(image_array, cmap='gray')
plt.axis('off')
plt.show()

2. Creating an image from a NumPy array:

Creating an image from a NumPy array using matplotlib.pyplot.imsave().

import numpy as np
import matplotlib.pyplot as plt

# Creating a NumPy array
image_array = np.random.random((100, 100))

# Saving the NumPy array as an image
plt.imsave('numpy_image.png', image_array, cmap='gray')

3. Display NumPy array as an image in Python:

Displaying a NumPy array as an image using matplotlib.pyplot.imshow().

import numpy as np
import matplotlib.pyplot as plt

# Creating a NumPy array
image_array = np.random.random((100, 100))

# Displaying the image
plt.imshow(image_array)
plt.axis('off')
plt.show()

4. Save NumPy array as an image file:

Saving a NumPy array as an image file using matplotlib.pyplot.imsave().

import numpy as np
import matplotlib.pyplot as plt

# Creating a NumPy array
image_array = np.random.random((100, 100))

# Saving the NumPy array as an image
plt.imsave('numpy_image.png', image_array)

5. Convert grayscale NumPy array to image:

Converting a grayscale NumPy array to an image using matplotlib.pyplot.imshow().

import numpy as np
import matplotlib.pyplot as plt

# Creating a grayscale NumPy array
gray_array = np.random.random((100, 100))

# Displaying the grayscale image
plt.imshow(gray_array, cmap='gray')
plt.axis('off')
plt.show()

6. Convert RGB NumPy array to image in Python:

Converting an RGB NumPy array to an image using matplotlib.pyplot.imshow().

import numpy as np
import matplotlib.pyplot as plt

# Creating an RGB NumPy array
rgb_array = np.random.random((100, 100, 3))

# Displaying the RGB image
plt.imshow(rgb_array)
plt.axis('off')
plt.show()

7. Image processing with NumPy arrays:

Performing simple image processing with NumPy arrays.

import numpy as np
import matplotlib.pyplot as plt

# Creating a simple image
image_array = np.random.random((100, 100))

# Applying image processing (e.g., thresholding)
processed_array = np.where(image_array > 0.5, 1.0, 0.0)

# Displaying the original and processed images
plt.subplot(1, 2, 1)
plt.imshow(image_array, cmap='gray')
plt.title('Original Image')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(processed_array, cmap='gray')
plt.title('Processed Image')
plt.axis('off')

plt.show()

8. Creating images from NumPy arrays using matplotlib:

Creating images from NumPy arrays using matplotlib.pyplot.imshow().

import numpy as np
import matplotlib.pyplot as plt

# Creating NumPy arrays
image_array_1 = np.random.random((100, 100))
image_array_2 = np.random.random((100, 100))

# Displaying images using matplotlib
plt.subplot(1, 2, 1)
plt.imshow(image_array_1, cmap='gray')
plt.title('Image 1')
plt.axis('off')

plt.subplot(1, 2, 2)
plt.imshow(image_array_2, cmap='gray')
plt.title('Image 2')
plt.axis('off')

plt.show()

9. Python PIL convert NumPy array to image:

Converting a NumPy array to an image using the Python Imaging Library (PIL).

import numpy as np
from PIL import Image

# Creating a NumPy array
image_array = np.random.random((100, 100)) * 255  # Assuming grayscale values between 0 and 255

# Converting NumPy array to PIL Image
image = Image.fromarray(image_array.astype(np.uint8))

# Displaying the image
image.show()