user6210457
user6210457

Reputation: 409

Convert Image into 1D Array in Python

I want to convert an image first into 2-D, then into 1-D array. I used reshape in order to do that. I converted image1.png which has 64x64x3 pixels into an array which has the size 1x12288. However, when I try to copy and display the first 100 values I get an empty array. Can you help me please?

Test Image(image1)

from PIL import Image
from scipy.misc import imread
import numpy as np

img1 = imread('image1.png')
img1 = np.reshape(img1,(128,96))

y = list(np.reshape(img1,(1,12288)))
z = y[1:101]

print(z)

Upvotes: 0

Views: 3807

Answers (2)

Daniel
Daniel

Reputation: 42778

You don't create a 1D-array, but a 2D-array with one row, and you try to get second to 100th row (Python indices are 0-based).

from PIL import Image
from scipy.misc import imread
import numpy as np

img1 = imread('image1.png')
y = list(img1.ravel())
z = y[:100]

print(z)

Upvotes: 2

lenik
lenik

Reputation: 23556

instead of

z = y[1:101]

you should try

z = y[0][1:101]

or make changes to reshape() call to make it really 1D array

Upvotes: 1

Related Questions