473,508 Members | 2,265 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python Imaging Library (PIL) question

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
Oct 20 '08 #1
2 1651
On Oct 20, 2:14*pm, Sid <sidmitra85-...@yahoo.co.inwrote:
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 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.

Oct 21 '08 #2
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
Oct 21 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2356
by: Fuzzyman | last post by:
I'll post this to the image-sig as well, but the audience is a bit wider here. I've just upgraded to Python 2.4. I've installed the free microsoft optimising compiler and hacked distutils to use...
0
1214
by: amaltasb | last post by:
Hi, I am hosting python applicaitions on a shared hositng so I dont have enough privilages, I have installed python in my home folder, through Automatic installer > extratools.php I want to...
2
1866
by: Tim Adler | last post by:
Hey everybody, I'm quite new to Python. I'm working on a webproject with Django and need to install the Python Imaging Library. It worked fine under Mac OS but when I try it on my Linux server....
2
2722
by: moishyyehuda | last post by:
can I download Python Imaging Library (PIL) for linux.
3
2160
by: bsagert | last post by:
I downloaded BeautifulSoup.py from http://www.crummy.com/software/BeautifulSoup/ and being a n00bie, I just placed it in my Windows c:\python25\lib\ file. When I type "import beautifulsoup" from...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5640
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4720
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.