简体   繁体   English

如何使用 PIL 获得灰度值?

[英]How can I get grayscale value with PIL?

I did a code to print all of the images pixel's grayscale values我做了一个代码来打印所有图像像素的灰度值

Here is my code:这是我的代码:

from PIL import Image, ImageColor
im = Image.open('upvote.png')
im = im.resize((50, 50))
im = im.convert('LA')#convert to grayscale
for i in range(50):
    print('\n')
    for j in range(50):
        pixel = im[i, j]# get pixel value
        print(pixel)

It is expected to get something like this:预计会得到这样的结果:
1 1 1 1 1 1 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 1 1 1 1 1
3 3 3 3 3 3 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 3 3 3 3 3
1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3
.... ....
depending on the image取决于图像

but I am getting this error:但我收到此错误:

TypeError: 'Image' object is not subscriptable

Alright, if anyone ever sees this here is how I solved it:好吧,如果有人看到这里是我解决它的方法:
Instead of this:取而代之的是:

pixel = im[i, j]

I used this:我用过这个:

pixel = im.getpixel((i, j))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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