简体   繁体   中英

I need to get the image matrix containing pixel values using pillow?

I am trying to run this simple code to have it output the image matrix of an image? I want it to output an image matrix with row and columns so that I can find the first rows/columns with nonzero pixel values so that I can crop... but right now my main problem is getting the matrix.

PLS&THANKYOU!

I keep getting this error:

Traceback (most recent call last): File "pillow_images.py", line 12, in matrix = np.array(im.getdata()).reshape(im.size) ValueError: cannot reshape array of size 147840 into shape (231,160)

import PIL #imports PIL library needed for pillow
from PIL import Image # imports Image class from pillow
import numpy as np

im = Image.open("whitewave.png") #loads in the image 

print(im.format, im.size, im.mode) #(PNG, (width,height), Type of image:RGB)

matrix = np.array(im.getdata()).reshape(im.size)
print(matrix)```

Use

matrix = np.array(im)

to get matrix with correct shape.


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM