Simon Shkolnik
Simon Shkolnik

Reputation: 398

How do I convert a PNG string into a Numpy array?

I need to convert a PNG image string into a numpy array. What are the best approaches?

Upvotes: 2

Views: 2248

Answers (2)

Zhiyuan Chen
Zhiyuan Chen

Reputation: 77

Tha question asked how to convert a PNG string into a numpy.ndarray, instead of how to read image from file system.

So the input should be a based 64 encoded string.

The best way is then to use

np.frombuffer(data, np.uint8)

Upvotes: 1

Nicolas Gervais
Nicolas Gervais

Reputation: 36674

You can use matplotlib for that:

import matplotlib.pyplot as plt

array = plt.imread('my_picture.png')

or PIL:

from PIL import image
import numpy as np

array = np.array(Image.open('my_picture.png'))

or cv2:

array = cv2.imread('my_picture.png')

or imageio:

import imageio
im = imageio.imread('my_picture.png')

Upvotes: 2

Related Questions