Python Imaging Library (PIL) question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sid

    Python Imaging Library (PIL) question

    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("Ima ges/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
  • Darcy Mason

    #2
    Re: Python Imaging Library (PIL) question

    On Oct 20, 2:14 pm, Sid <sidmitra85-...@yahoo.co.in 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("Ima ges/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 asabove, 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/tut...er/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.

    Comment

    • Sid

      #3
      Re: Python Imaging Library (PIL) question

      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

      Comment

      Working...