Optimize iteration throught numpy array
iteration, loops, numpy, opencv, python
Solution
How about this single operation inverting the matrix along the last axis?
cv_image = cv_image[:,:,::-1]
Problem
I'm swapping values of a multidimensional numpy array in Python. But the code is too slow. Another thread says: Typically, you avoid iterating through them directly. ... there's a good chance that it's easy to vectorize. So, do you know a way to optimize the following code? ``` import PIL.Image import numpy pil_image = PIL.Image.open('Image.jpg').convert('RGB') cv_image = numpy.array(pil_image) # Convert RGB to BGR for y in range(len(cv_image)): for x in range(len(cv_image[y])): (cv_image[y][x][0], cv_image[y][x][2]) = (cv_image[y][x][2], cv_image[y][x][0]) ``` For an 509x359 image this last more than one second, which is way too much. It should perform it's task in no time.