PIL Image getpixel TypeError: 'int' object is not iterable

Viewed 5123

I want to use the getpixel((w,h)) method of PIL.Image but we processing image this give a

TypeError: 'int' object is not iterable

Error, it seems that method don't return a three elements tuple, have tried with both png file and jpg file same result, what happend?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from PIL import Image

def makeIndex():
    pic = Image.open('lines.jpg')
    width = pic.width
    height = pic.height
    for w in range(0, width):
        for h in range(0, height):
            r,g,b= pic.getpixel((w,h))
            print (r,g,b)



if __name__ == "__main__":
    makeIndex()

# vim:ai:et:sts=4:sw=4:

EDIT

as mentioned by the comment this time I chose another png file to do the test and do a convert('RGB') before processing

    pic = Image.open('stencil.png').convert('RGB')
    width = pic.width
    height = pic.height
    for w in range(0, width):
        for h in range(0, height):
            r,g,b= pic.getpixel((w,h))
            print (w,h), (r,g,b)

And this give a result of

(106, 988) (141, 60, 146)
(106, 989) (141, 60, 146)
(106, 990) (141, 60, 146)
(106, 991) (141, 60, 146)
(106, 992) (130, 36, 36)
(106, 993) (130, 36, 36)
(106, 994) (130, 36, 36)
(106, 995) (130, 36, 36)
(106, 996) (130, 36, 36)
(106, 997) (130, 36, 36)
(106, 998) (130, 36, 36)
(106, 999) (130, 36, 36)

And each has different r,g,b value

So what does this layer things really mean?

enter image description here

0 Answers
Related