http://qs1969.pair.com?node_id=70931

atcroft has asked for the wisdom of the Perl Monks concerning the following question:

I humbly beseech your knowledge, as I have been to the meager limits of my search abilities but to no avail.

I am using 'use Tk;' and the Canvas object for creating displays on a screen. I am looking for a way to manipulate it at a pixel-level for parts of what I need to do. So far, I finally found a way to create an individual pixel (CreateText, using a '.' -is there a better way), but I cannot seem to find information on:
(a) how to create an savable image from a Canvas, or
(b) how to get the color of a pixel at a particular location on a Canvas.

Your guidance I humbly seek.

-----
A journey of a 1000 lines of code begins with a single keystroke.
  • Comment on Question about Tk Canvas, pixels, and images

Replies are listed 'Best First'.
Re: Question about Tk Canvas, pixels, and images
by Dr. Mu (Hermit) on Apr 09, 2001 at 10:45 UTC
    Use Tk::Photo to create a blank image and then the Canvas' createImage method to install it onto the Canvas. Once there, you can manipulate its pixels individually, like this:
    use Tk; $mw = Tk::MainWindow->new; $can = $mw->Canvas(-width => 320, -height => 240)->pack(); $img = $mw->Photo(-width => 320, -height => 240, -palette => '256/256/ +256'); $img->blank; $can->createImage(0, 0, -image => $img, -anchor => 'nw'); $img->put(RED, -to => (160, 120)); printf "%6.6X", $img->get(160, 120); MainLoop();
    This code puts a red dot in the middle of a transparent image, then reads and outputs its color in hex. The documentation that covers this comes with the ActiveState distribution of Tk, but I'm sure you can find it on the web if you're not using that distro.