Ryan
Ryan

Reputation: 10149

Opening a single image

I'm trying to open an image with size (520,696) but when I use this

array = np.array([np.array(Image.open(folder_path+folders+'/'+'images'+'/'+image))], np.int32).shape`

I'm getting the shape as

(1, 520, 696, 4)

The problem is with this shape I can't convert it to image using toimage(array); I get

'arr' does not have a suitable array shape for any mode.

Any suggestions on how may I read that image using only (520,696)?

Upvotes: 0

Views: 196

Answers (2)

Mr. T
Mr. T

Reputation: 12410

You should load the picture as a single picture instead of loading it as a stack and then removing the irrelevant stack dimension. The basic procedure could be something like this:

from PIL import Image

pic = Image.open("test.jpg")
pic.show()                     #yup, that's the picture

arr = np.array(pic)            #convert it to a numpy array
print(arr.shape, arr.dtype)    #dimension and data type
arr //= 2                      #now manipulate this array

new_pic = Image.fromarray(arr) #and keep it for later
new_pic.save("newpic.bmp")     #maybe in a different format

Upvotes: 1

llllllllll
llllllllll

Reputation: 16434

The problem is the additional dumb dimension. You can remove it using:

arr = np.squeeze(arr)

Upvotes: 3

Related Questions