我试着从屏幕截图的中心看像素。我正在使用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是否可以在不将迭代器读取到新对象的情况下增加迭代器?这个操作在快速工作站上为768x1280PNG(它确实有一个阿尔法通道)花费了2秒时间。
发布于 2014-10-23 08:58:42
您可以使用迭代工具中的 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)https://stackoverflow.com/questions/26524458
复制相似问题