On Oct 20, 2:14 pm, Sid <[EMAIL PROTECTED]> wrote: > Hi, > > I am tryin to copy an image into my own data structure(a sort of 2d array > for further FFT). I've banged my head over the code for a couple of hours > now. The simplified version of my problem is below. > > #-----------------Code------------------------ > > import Image > pic = Image.open("Images/a.jpg") > (picdata,width,height) = (pic.load(),pic.size[0],pic.size[1]) > > picMap = [[0] * width ] * height #matrix needed for FFT > > print "------Printing PIC MAP------" > for h in range(0,height): > for w in range(0,width): > print picMap[h][w]," ", #everything initialized to ZERO...this is > ok > print "\n" > > print "------Copying to PIC MAP------" > for h in range(0, height): > for w in range(0, width): > picMap[h][w] = picdata[w,h] #problem lies here???? > print picMap[h][w]," ", #Seems to copy perfectly here as the > output shows > print "\n" > > print "------Printing PIC MAP AGAIN------" > for h in range(0,height): > for w in range(0,width): > print picMap[h][w]," ", #Should print the values as above, but > doesn't > print "\n" > > #-----------------Code End------------------------ > > Hopefully someone would take a look and let me know what i'm doing > something wrong here. > > Thanks a lot > -Sid
The problem is likely to do with the line picMap = [[0] * width ] * height The first [0]*width creates a list, then the *height repeats the reference to the same list. See, for example http://mail.python.org/pipermail/tutor/2001-December/010414.html. It prints okay in the middle section because the items have just been stored. Later iterations in that same loop are replacing array elements that have already been printed. The link explains the problem and shows a way to avoid this. You might also consider using Numpy, it has explicit functions to zero any dimension array. -- http://mail.python.org/mailman/listinfo/python-list