[Tutor] plotting pixels

Lie Ryan lie.1296 at gmail.com
Sun Sep 19 09:40:34 CEST 2010


On 09/19/10 09:39, ALAN GAULD wrote:
>> It appears that the Tk canvas widget does not support simply plotting
> a pixel. 
> 
> Correct, and I agree it seems odd, but in practice drawing either lines or
> ovals of one-pixel do the equivalent job - albeit a little more slowly.

More slowly and takes huge amount of memory. A single Tk canvas object
takes at least 14 words (= 114 bytes in 64-bit OS = 56 bytes in 32-bit
OS) + the amount of data is needed to store the `kind of object`. That's
much larger than the ideal 3 bytes per pixel (or 4 bytes with alpha).

Tkinter's Canvas intentionally doesn't provide create_pixel() because
unlike most other Canvas implementations, Tkinter's Canvas holds a
stateful canvas objects instead of a simple drawing surface. Providing a
create_pixel() will be too tempting for abuse, which would make the
canvas unbearably slow and memory consuming. In short, Canvas is not
designed for pixel drawing.

> Digging a little deeper it seems the idiomatic way to do this in Python
> is to use PIL the Python Imaging Library to create a GIF or bitmap
> image and then insert that into Tkinters cancvas as an image object.
> 
> The Pil ImageDraw class has a point() ethod

If you need to plot pixels, do use pygame, PIL, or at least the
PhotoImage trick.



More information about the Tutor mailing list