[PIL] about the difference between pudata and putpixel

H

Hillairet Julien

Hello,

I don't understand how "putdata" from the PIL works :

I've got a (N,N) matrix : "color" (an array from numarray lib.), where
(N,N) is the size of a image.

The next code, give to each pixel of the image the color (an integer
0~255) which is associate in the "color" matrix (an integer matrix,
0~255). It works well, but it's a bit slow:

for i in range(N):
for j in range(N):
im.putpixel((i,j), color[j])

Although, this code doesn't work:

im.putdata(color)

There is no error message, but the image's pixels stay black (0) ! (The
color matrix is not an 0-matrix !)


Is someone can explain me my misunderstanding of putdata ?


Thanks a lot,

Julien Hillairet
 
T

Tom B.

Hillairet Julien said:
Hello,

I don't understand how "putdata" from the PIL works :

I've got a (N,N) matrix : "color" (an array from numarray lib.), where
(N,N) is the size of a image.

The next code, give to each pixel of the image the color (an integer
0~255) which is associate in the "color" matrix (an integer matrix,
0~255). It works well, but it's a bit slow:

for i in range(N):
for j in range(N):
im.putpixel((i,j), color[j])

Although, this code doesn't work:

im.putdata(color)

There is no error message, but the image's pixels stay black (0) ! (The
color matrix is not an 0-matrix !)


Is someone can explain me my misunderstanding of putdata ?


Thanks a lot,

Julien Hillairet


Check your picture mode.

import Image
import random

pyimg = Image.new('RGB',(10,10))
ll = []
for i in range(100):

ll.append((random.randint(1,255),random.randint(1,255),random.randint(1,255)
)) # a tuple of RGB values
pyimg.putdata(ll)
pyimg.show() # drag open the pill view window to see (its not large enough)


Tom
 
K

Kylotan

Hillairet Julien said:
Although, this code doesn't work:

im.putdata(color)

There is no error message, but the image's pixels stay black (0) ! (The
color matrix is not an 0-matrix !)

Is someone can explain me my misunderstanding of putdata ?

I think the misunderstanding is of numarray. I'm guessing that
iterating through a 2D array returns individual rows rather than
individual elements.

I've never used numarray before but from looking at the documentation,
you could try this:

im.putdata(color.ravel())

or

im.putdata(ravel(color))
 
H

Hillairet Julien

Le Sun, 29 Aug 2004 07:37:37 -0700, Kylotan a écrit :
I think the misunderstanding is of numarray. I'm guessing that
iterating through a 2D array returns individual rows rather than
individual elements.

I've never used numarray before but from looking at the documentation,
you could try this:

im.putdata(color.ravel())

or

im.putdata(ravel(color))

Yes that's it !!

I've not understood that Image need an 1D array...

Thanks a lot.
 

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
474,204
Messages
2,571,065
Members
47,672
Latest member
svaraho

Latest Threads

Top