Python Imaging Library (PIL) question

S

Sid

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
 
D

Darcy Mason

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.
 
S

Sid

The problem is likely to do with the line
picMap = [[0] * width ] * height

yeah the problem was with that specific line. The snippet from your link
worked by
replacing the previous declaration by

picMap = [[0] * 3 for x in xrange(height)]

Thanks,
-Sid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top