How can I process images faster with Python?

2024/10/10 14:30:19

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?

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!

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 Imagex = 0
y = 0im = 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 imageprint "x is:" +str(x)x = x + 1print "y is: " +str(y)if x == 1920:x = 0y = y + 1
Answer

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.

https://en.xdnf.cn/q/69886.html

Related Q&A

KFolds Cross Validation vs train_test_split

I just built my first random forest classifier today and I am trying to improve its performance. I was reading about how cross-validation is important to avoid overfitting of data and hence obtain bett…

Using Keras, how can I input an X_train of images (more than a thousand images)?

My application is accident-avoidance car systems using Machine Learning (Convolutional Neural Networks). My images are 200x100 JPG images and the output is an array of 4 elements: the car would move le…

Fastest way to merge two deques

Exist a faster way to merge two deques than this?# a, b are two deques. The maximum length # of a is greater than the current length # of a plus the current length of bwhile len(b):a.append(b.poplef…

Python cannot find shared library in cron

My Python script runs well in the shell. However when I cron it (under my own account) it gives me the following error:/usr/local/bin/python: error while loading shared libraries: libpython2.7.so.1.0: …

Multiple async unit tests fail, but running them one by one will pass

I have two unit tests, if I run them one by one, they pass. If I run them at class level, one pass and the other one fails at response = await ac.post( with the error message: RuntimeError: Event loop…

Pyusb on Windows 7 cannot find any devices

So I installed Pyusb 1.0.0-alpha-1 Under Windows, I cannot get any handles to usb devices.>>> import usb.core >>> print usb.core.find() NoneI do have 1 usb device plugged in(idVendor=…

How to speed up nested for loops in Python

I have the following Python 2.7 code:listOfLists = [] for l1_index, l1 in enumerate(L1):list = []for l2 in L2:for l3_index,l3 in enumerate(L3):if (L4[l2-1] == l3):value = L5[l2-1] * l1[l3_index]list.ap…

Folium Search Plugin No Results for FeatureGroup

Im trying to add search functionality to a map Im generating in Python with Folium. I see there is a handy Search plugin available and able to implement it successfully and get it added to the map. Unf…

writing dictionary of dictionaries to .csv file in a particular format

I am generating a dictionary out of multiple .csv files and it looks like this (example):dtDict = {AV-IM-1-13991730: {6/1/2014 0:10: 0.96,6/1/2014 0:15: 0.92,6/1/2014 0:20: 0.97},AV-IM-1-13991731: {6/1…

How to import SSL certificates for Firefox with Selenium [in Python]?

Trying to find a way to install a particular SSL certificate in Firefox with Selenium, using the Python WebDriver and FirefoxProfile. We need to use our own, custom certificate which is stored in the …