1

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

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.