Search code examples
pythonpython-3.xfor-looppython-imaging-librarypixel

Why isn't this loop working properly?


So basically I am trying to rearrange certain pixels within an image. The pixels to be rearranged are stored in two lists called xpixels and ypixels, where a pair from both corresponds to the pixel coordinate to be rearranged in the original image. However, my function is only rearranging the pixels on the y axis, and only for the first column. I'm not really sure why this is happening, can only think that the outer loop is not being processed correctly for some reason, or just faulty logic on my part. Any help would be appreciated.

    for i in range(x1, x2): #range of x coordinates in original image

        for j in range(y1, y2): #range of y coordinates in original image
            if ycounter >= len(ypixels):
                break
            else:
        #pixmap is a pixelaccess object containing image data    
                pixmap[i, j]=pixmap[i, ypixels[ycounter]] #this shuffles the first y column
                ycounter=ycounter+1

Solution

  • I'm not entirely sure what this code is doing, but I guess you need to reset the value of ycounter at the start of each iteration of the outer loop.