## Introduction:

When you want to play with pictures using a computer, you need a way to turn them into something the computer understands. In this blog, we're going to learn simple ways to change images into a special kind of numbers called NumPy arrays. We will use easy-to-understand Python code for each method.

## Method 1: Using the PIL Library

The PIL Library is like a magic wand for pictures in Python. It helps us open, change, and save different kinds of pictures. Let's see how we can use it to turn a picture into a set of numbers.

```
from PIL import Image
import numpy as np
# Open a picture file
image_path = "path/to/your/image.jpg"
image_pil = Image.open(image_path)
# Turn the picture into a set of numbers (NumPy array)
image_np = np.array(image_pil)
# Show the set of numbers
print(image_np)
```

### Output:

The output is a bunch of numbers that represent the picture.

## Method 2: Using OpenCV

OpenCV is like a super-helper for working with pictures and videos. It has a cool trick to quickly turn a picture into numbers.

```
import cv2
import numpy as np
# Read a picture using OpenCV
image_path = "path/to/your/image.jpg"
image_cv2 = cv2.imread(image_path)
# Turn the OpenCV picture into a set of numbers (NumPy array)
image_np_opencv = np.array(image_cv2)
# Show the set of numbers
print(image_np_opencv)
```

### Output:

The output is another set of numbers that represent the picture, just like in Method 1.

## Method 3: Using Matplotlib

Matplotlib is like an artist's toolkit for pictures. It can also help us turn pictures into numbers. Let's see how easy it is.

```
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read a picture using Matplotlib
image_path = "path/to/your/image.jpg"
image_matplotlib = mpimg.imread(image_path)
# Turn the Matplotlib picture into a set of numbers (NumPy array)
image_np_matplotlib = np.array(image_matplotlib)
# Show the set of numbers
print(image_np_matplotlib)
```

### Output:

The output is a set of numbers again, just like in the previous methods.

## Method 4: Using Scikit-Image

Scikit-Image is like a secret agent for pictures. It helps us do special things with pictures, and it has a neat trick to turn them into numbers.

```
from skimage import io
import numpy as np
# Read a picture using Scikit-Image
image_path = "path/to/your/image.jpg"
image_skimage = io.imread(image_path)
# Turn the Scikit-Image picture into a set of numbers (NumPy array)
image_np_skimage = np.array(image_skimage)
# Show the set of numbers
print(image_np_skimage)
```

### Output:

Once again, the output is a set of numbers that represent the picture.

## Conclusion:

In this blog, we have learned simple ways to change pictures into numbers using different ways in Python like the PIL, OpenCV, the Matplotlib, or the Scikit-Image.

## Comments (0)