I want to load a .PNG into a 2D structure of 1D structures of RGB values. For example, a 16x16
image would be represented as a 16x16x3
array, where the deepest subarray of length 3
is the RGB values.
I know of PIL
and numpy
, and tried something along the lines of:
from PIL import Image
from numpy import asarray
# ...
loaded_image = asarray(Image.open('filename.png'))
This does indeed make a 16x16x3
numpy
array, but its of values between 0..255
. I try to then iterate over them and make them between 0.00..1.00
:
for row in range(len(loaded_image)):
for col in range(len(loaded_image[0])):
for channel in range(len(loaded_image[0][0])):
loaded_image[row][col][channel] /= 256
But this tells me the array is read-only.
Is there a better method for this? If it matters, I only really care about greyscale (R = G = B
), so it could actually be a 16x16x1
structure. Is there a method using Python standard libraries?