简体   繁体   中英

Python iterate through pixels of image

I'm trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Image

w=100
h=200
im=im.resize((w,h), Image.ANTIALIAS)
for a in w:
    for b in h:
    (...)

The type error issue is coming from the fact that w and h and integers and therefore cannot be iterated through. The construct for i in thing: loops through every item in thing , so if thing is a list [2, 5, 6] , for example, i will be 2 and then 5 and then 6.

What you want is for a in range(w) and for b in range(h) , which will allow you to iterate through all integers from 0 to w or h .

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