[Image-SIG] Pixmap widget with access to pixels?

Kevin Cazabon KCAZA@cymbolic.com
Tue, 28 Mar 2000 18:06:18 -0800


Well, in PIL, you can set an individual pixel very easily.  If you want to display
these pixels as they're updated, it's easy too.

How about something like this:

#########################
import Image, ImageTk
from Tkinter import *

class displayPixMap:
    def __init__(self, parent, imageSize):
        self.image = Image.new("RGB", imageSize, (0,0,0))
        self.TkImage = ImageTk.PhotoImage(self.image)
        self.imageDisplay = Label(parent, image = self.TkImage)
        self.imageDisplay.pack()

    def putPixel(self, x, y, value):
        self.image.putpixel((x,y), value)
        self.TkImage = ImageTk.PhotoImage(self.image)
        self.imageDisplay.update_idletasks()

test = Frame()
test.pack()
im = displayPixMap(test, (100,100))

for y in range(100):
    for x in range(100):
        value = FooBar(x, y) # Where FooBar returns a 3-tuple of RGB values
        im.putPixel(x, y, value)


test.mainloop()


#########################


>>> Ralph Seguin <rpseguin@yahoo.com> 03/28/00 03:10PM >>>
> What exactly do you mean by "direct access to
> pixels"? If you want to

I mean being able to set a given pixel a given color.
For each pixel in a canvas.
Fractals, surface plots, graphics rendering, ray
trace, ...
I can see a number of things that I could use this
for.

for curY in range ( 0, 1000 ) :
 for curX in range ( 0, 1000 ) :
  # fooBar () is some time-consuming
  # function which will compute a color
  # value, possibly/probably unique per
  # pixel.
  val = fooBar( curX, curY )
  myCanvas.setPixel( curX, curY, val )

In this example, fooBar() is some arbitrary,
time-consuming function, so the cost of doing
setPixel() is small in comparison.

Doing this by generating degenerate (Canvas)
rectangles or
degenerate (Canvas) lines is very inefficient in both
storage space and compute/render time.

Currently, I can use PIL to render to an off-screen
image and then assign this image to the image for a
Tkinter.Label or other widget, but this is really not
good either.

> exercise is futile: Python is simply too slow for
> that. (Exception are 

Setting a pixel represents a minor fraction of the
amount of time for computing some things like
fractals, ray traces, ...

> night sky after computing it you can get away with
> it because there's so few pixels to paint).

??

> Whether you have direct access to slightly higher
> abstractions (like
> scanlines or rectangular areas) depends on the gui

Having a rectangle for each pixel is incredibly
inneficient.

> interesting: it won't help you much if I tell you
> the raw bitmap for any window from MacPython if
> you're not using a mac:-)

I'd like to be platform independent.

Since I do most of my work under UNIX, I could hack up
some code to write pixels directly to X drawables, but
this is hardly elegant or the way that I would like to
do it.

Thanks.
-Ralph



__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com 

_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org 
http://www.python.org/mailman/listinfo/image-sig