如何使用Python更快地处理图像? [英] How can I process images faster with Python?

查看:137
本文介绍了如何使用Python更快地处理图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个脚本,该脚本将检测屏幕上的RGB值,然后单击x,y值.我知道如何执行点击,但是我需要比下面的代码更快地处理图像. Python可以做到这一点吗?

I'd trying to write a script that will detect an RGB value on the screen then click the x,y values. I know how to perform the click but I need to process the image a lot faster than my code below currently does. Is this possible with Python?

到目前为止,我一次读取一行,当x = 1920时,我进入第二行,但是一行大约需要10秒钟.到那时,屏幕上的那个人会搬到一个完全不同的地方,而我只做了一行!

So far I'm reading a row at a time, when x = 1920 I go onto the second row but it takes about 10 seconds to do one row. By that time the person on screen would have moved into a completely different spot and I have only done one row!

我可以加快这段代码的速度吗,或者有没有更好的方法来实现我想要的?如果无法在Python中使用,我可以使用C ++选项:)

Can I speed this code up OR is there a better way to achieve what I want? If it is not possible in Python I am open to C++ options :)

import Image

x = 0
y = 0

im = Image.open("C:\Users\sean\Desktop\screen.jpg")
pix = im.load()
print im.size #get width and height of the image for iterating over
while x < 1920:
    print pix[x,y] #get RGBA value of the pixel of an image
    print "x is:" +str(x)
    x = x + 1
    print "y is: " +str(y)
    if x == 1920:
        x = 0
        y = y + 1

推荐答案

通常,您希望避免Python中的逐像素循环.他们总是很慢.为了获得更快的图像处理速度,您需要习惯使用矩阵而不是单个像素.您基本上有两个选择,可以使用NumPy或OpenCV,也可以将两者结合使用. NumPy是一个通用的数学矩阵/数组库,但是您可以使用它来做很多与图像有关的事情.如果您需要更具体的内容,OpenCV支持对图像的许多常见操作.

Generally, you want to avoid per-pixel loops in Python. They will always be slow. To get somewhat fast image processing, you need to get used to working with matrices instead of individual pixels. You have basically two options, you can either use NumPy or OpenCV, or a combination of the two. NumPy is a generic mathemtical matrix/array library, but you can do many image-related things with it. If you need something more specific, OpenCV supports many common operations on images.

这篇关于如何使用Python更快地处理图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆