简体   繁体   中英

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

我不完全确定这段代码在做什么,但是我想您需要在每次外部循环迭代开始时重置ycounter的值。

I'm still trying to figure out what your problem consist of, but i tried to visualize it:

pixmap = [[0 for x in range(5)] for x in range(5)]
for i in range(5):
    for j in range(5):
        pixmap[i][j] = '1'


for i in range(5):
    pixmap[j][i] = '0'

print "Original Matrix:"
for i in range(5):
    print pixmap[i]

def rearrange_funct(oldPos,newPos):
    original_posX, original_posY = oldPos
    rearrange_posX, rearrange_posY = newPos
    oldContent = pixmap[original_posX][original_posY]
    pixmap[original_posX][original_posY] = pixmap[rearrange_posX][rearrange_posY]
    pixmap[rearrange_posX][rearrange_posY] = oldContent



oldPixelPositions = [(0,0),(0,1),(0,2),(0,3),(0,4)]

newPixelPositions = [(4,0),(4,1),(4,2),(4,3),(4,4)]
for x in range(len(oldPixelPositions)):
    rearrange_funct(oldPixelPositions[x],newPixelPositions[x])

print "Rearranged Matrix:"
for i in range(5):
    print pixmap[i]

Is that what you're trying to achieve ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM