简体   繁体   中英

python image size function in PIL

I am using the PIL package in python and I want to import the pixels into a matrix after I convert it to grayscale this is my code

from PIL import Image
import numpy as np

imo = Image.open("/home/gauss/Pictures/images.jpg")

imo2 = imo.convert('L')
dim = imo2.size
pic_mat = np.zeros(shape=(dim[0] , dim[1]))

for i in range(dim[0]):
    for j in range(dim[1]):
        pic_mat[i][j] = imo2.getpixel((i,j))

My question is about the size function. it usually returns a tuple (a,b) where a is the width of the picture and the b is the length of the picture, but doesn't that mean that a is the column in a matrix and b is the row in a matrix. I am wondering this to see if I set up my matrix properly.

Thank you

Try just doing

pic_mat = np.array(imo.convert('L'))

You can also avoid doing things like shape=(dim[0] , dim[1]) by slicing the size tuple like this shape=dim[:2] (the :2 is even redundant in this case but I like to be careful...)

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