Python迭代图像的像素 [英] Python iterate through pixels of image
问题描述
我正在尝试迭代图像的像素。我设置大小然后使用for循环,但是我得到一个类型错误:对象不可迭代。我已导入PIL和图像
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:
(...)
推荐答案
类型错误问题来自于 w
和 h
和整数因此无法迭代。在物品中的构造:
循环遍历 thing
中的每一项,所以如果 thing
是一个列表 [2,5,6]
,例如, i
将是2然后是5,然后是6。
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.
对于范围内(w)和<$ c,你想要的是 $ c> for b in range(h)
,这将允许您遍历从0到 w
或 h
。
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
.
这篇关于Python迭代图像的像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!