old: if pixAlpha >= alpha_ignore: for colour_id in filtered_colours_to_filter: .. new: pixcache = dict() // before y loop if pixAlpha >= alpha_ignore: winnerID = pixcache.get(pix) if not winnerID: for colour_id in filtered_colours_to_filter: .. pixcache[pix] = winnerID;