我正在尝试查看屏幕截图中心的像素。我正在使用PngPy来阅读屏幕截图,并想快速找到像素。
import png
import itertools
r=png.Reader("myfile.png")
direct = r.asRGBA8()
bytesIter = direct[2] # returns itertools.imap object -
# see https://pythonhosted.org/pypng/png.html
height = direct[1]
count=0
for row in bytesIter:
if count >= (height/2):
print "Half way"
break
count+=1
print count
无论如何都要增加迭代器而不将其读取到新对象? 对于快速工作站上的768x1280 png(具有Alpha通道),此操作需要2秒钟。
答案 0 :(得分:1)
您可以使用itertools中的consume
recipe:
from itertools import islice
from collections import deque
def consume(iterator, n):
"Advance the iterator n-steps ahead. If n is none, consume entirely."
# Use functions that consume iterators at C speed.
if n is None:
# feed the entire iterator into a zero-length deque
deque(iterator, maxlen=0)
else:
# advance to the empty slice starting at position n
next(islice(iterator, n, n), None)
所以,在你的情况下:
consume(bytesIter, height/2)