[Image-SIG] Low level access

Robert Klimek klimek@grc.nasa.gov
Thu, 11 Jul 2002 11:07:51 -0400


On Wednesday 10 July 2002 11:05 am, you wrote:
> Hi,
>   I've just been playing with the PIL and I'm quite impressed.  It's my
> first use of python, so I've been having lots of fun.  I do have a ques=
tion
> though...
>
> I've been writing a tool that alters an image based on the content of
> another. The first way I tried it, I hust used getpixel and putpixel.  =
This
> worked fine, but it was very very slow.  I speeded things up a little
> (actually, I doubled the speed) by using getdata to retrieve the pixel =
info
> from the two files.  What I then wanted to do was to remove the use of
> putpixel.  First I thought I'd use getdata to get an object of the righ=
t
> size and then alter it for the new image, but I can't (it's immutable?)=
=2E=20
> Is there any way to quickly manipulate the image data at the pixel leve=
l?=20
> I've been using the fromstring method to speed things up a little (crea=
ting
> a list and then joining it to create the string rather than using
> concatenation), but it's hardly a big improvement.
>
> Am I going about this in the wrong way?  Is PIL the right tool for the =
job?
> It's taking around 23s per image pair, which is just too slow.  Ideally=
,
> this is intended for frame by frame compositing of rendered movie passe=
s.
>
> Any ideas?
>

I'm often frustrated by this as well, where you need to loop through all =
the=20
pixels of an image and perform some operation, like on a 3x3 neighborhood=
=2E=20
I'm currently looking at a floodfill operation and I'm running into the s=
peed=20
problem. The one thing in PIL that is very fast is the point() function. =
It=20
may be coupled with lambda such as this simple threshold operation below:

def Threshold(image, value=3D128):
    # set to 255 if pixel greater than value
    t =3D image.point(lambda i, v=3Dvalue: i > v and 255)
    return t


Maybe you can modify it to suit your requirement? However, there are many=
=20
situations where I don't know how to implement this scheme, such as the o=
ne=20
you're requesting.

Hope this helps!
Bob