Python Pillow – M L with Numpy


Python Pillow – ML with Numpy



”;


Image manipulation with NumPy is a common practice in image processing tasks. NumPy provides a powerful array manipulation library that complements Pillow”s image processing capabilities. This tutorial demonstrates how to use Pillow with NumPy for efficient image processing.

Installation

Before proceeding, ensure we have NumPy installed. Open the command prompt in administrator mode and execute the following command −


pip install numpy

Note − This command works only if you have PIP installed and updated.

Creating image from Numpy Array

When working with NumPy arrays as images, we can use the Image.fromarray() function to create an image memory from an object that exports the array interface, typically using the buffer protocol. If the input array (obj) is contiguous in memory, Pillow can use the array interface directly. If the array is not contiguous, Pillow will use the tobytes method, and frombuffer() will be used to create the image. Here”s the syntax of the fromarray() function −


PIL.Image.fromarray(obj, mode=None)

Where,

  • obj − The object exporting the array interface. This is usually a NumPy array, but it can be any object that exposes the required interface.

  • mode (optional) − The mode parameter specifies the color mode or pixel format of the resulting image. If not provided, the mode is inferred from the type of the input array.

It”s important to note that Pillow modes (color modes) do not always correspond directly to NumPy data types (dtypes). Pillow modes include options for 1-bit pixels, 8-bit pixels, 32-bit signed integer pixels, and 32-bit floating-point pixels. The mode is either explicitly specified or inferred from the dtype of the input array.

Example

In this example, a NumPy array is created, and then Image.fromarray() is used to create a Pillow Image from the NumPy array. The resulting image is a Pillow Image object that can be further processed or saved.


from PIL import Image
import numpy as np

# Create a NumPy array
arr = np.zeros([150, 250, 3], dtype=np.uint8)
arr[:,:100] = [255, 128, 0]
arr[:,100:] = [0, 0, 255]

# Create a Pillow Image from the NumPy array
image = Image.fromarray(arr)

# Display the created image
image.show()

Output


created image

Example

Here is another example that create a Pillow Image from the NumPy array by explicitly specifying the mode.


from PIL import Image
import numpy as np

# Create a NumPy array
arr = np.zeros([250, 350, 3], dtype=np.uint8)
arr[:100, :200] = 250

# Create a Pillow Image from the NumPy array by explicitly specifying the mode
image = Image.fromarray(arr, mode=''RGB'')

# Display the created image
image.show()

Output


created image black and white

Example

This example creates a grayscale image from a numpy 2-dimensional array by explicitly specifying the mode equal to “L”.


from PIL import Image
import numpy as np

# Create a NumPy array
arr = np.zeros([300, 700], dtype=np.uint8)
arr[100:200, 100:600] = 250

# Create a Pillow grayscale Image from the NumPy array 
# by explicitly specifying the mode
image = Image.fromarray(arr, mode=''L'')
print("Pixel values of image at (150, 150) of the grayscale image is:", image.getpixel((150, 150)))

# Display the created image
image.show()

Output


Pixel values of image at (150, 150) of the grayscale image is: 250


grayscale image

Creating numpy array from a Pillow Image

The numpy.asarray() function can be used to convert a Pillow image to a NumPy array. However, it”s important to note that when converting Pillow images to arrays, only the pixel values are transferred. This means that certain image modes, like P and PA, will lose their palette information during the conversion.

Example

The following example demonstrates how to convert a Pillow image to a NumPy array.


from PIL import Image
import numpy as np

# Open an image as a pillow image object
image = Image.open("Images/TP logo.jpg")

# Convert the Pillow image to a NumPy array
result = np.asarray(image)

# Display the type, shape and dtype of the NumPy array
print("Type:", type(result))
print("Shape:", result.shape)
print("Dtype:", result.dtype)

Output


Type: <class ''numpy.ndarray''>
Shape: (225, 225, 3)
Dtype: uint8

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *