9951 explained code solutions for 126 technologies


python-numpyImport image data to Numpy array


import numpy as np
from PIL import Image

image = Image.open('/var/www/examples/heroine.png')
data = np.asarray(image)ctrl + c
import numpy as np

load Numpy module for Python

from PIL import Image

load Pillow module to work with images

Image.open

loads given image into image array

/var/www/examples/heroine.png

path to image to load

np.asarray

load Numpy array data from given data treated as array

data

will contain data loaded from image


Usage example

import numpy as np
from PIL import Image

image = Image.open('/var/www/examples/heroine.png')
data = np.asarray(image)

print(data.shape)
print(data[1][3][154])
output
(1100, 733, 3)