kolonel
kolonel

Reputation: 1422

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

Upvotes: 1

Views: 2539

Answers (1)

YXD
YXD

Reputation: 32511

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...)

Upvotes: 2

Related Questions