I am doing some pixel operations in Python, which obviously uses a lot of processing power and makes the program extremely slow. I have these nested loops:
for x in range(-1, 2): #for the left, center and right row of pixels next to this pixel for y in range(-1, 2): #for the top and bottom row of pixels nwxt to this pixel if x == 0 and y == 0: #exclude this pixel pass else: for pix in allPixels: #for every existing pixel surroundingpixel = False pix_info = pix.info() #get the pixel information if pix_info == self.x + x and pix_info == self.y + y: #if there is a pixel in this position self.surr.append(pix) #add the pixel to the list of surrounding pixels surroundingpixel = True if not surroundingpixel: self.surr.append(0) return self.surr
If there are a lot of pixels on the screen the program slows down massively My full code is quite a bit longer, but I can add things to this post as necessary.
I was hoping someone could help me make this code more efficient. I've been using nested loops and using cProfiler, 86% of processing time is taken up by this function. Any help making this block of code more efficient would be much appreciated.
The full code can be found here if needed.