Search code examples
perltk

PerlTk canvas + pixel manipulation


I'm having some problems understanding how the image types in PerlTk work.

I basically want a way to create an "empty" image (for example fully white) and then manipulate the pixel data. I need to able to change a pixel to black or white.

I've tried a few approaches but non of them seem to work. Documentation seems to be pretty scarce and very old. This is closest I've got.

#args name, width, height, data
my $bitmap = $mw->DefineBitmap('cells', 1, 1, pack("b1", "1")); 

#args x-pos, y-pos, bitmap-name 
$canvas->createBitmap(0, 0, -bitmap => 'cells'); 

Another idea I had was to use a Photo element but I couldn't find any documentation on how to create one with the "-data" option.

Any help is appreciated.


Solution

  • Use the put() method if you have to set single pixels. Here's a complete example:

    use Tk;
    my $mw = tkinit;
    my $p = $mw->Photo(-width => 100, height => 100);
    my $l = $mw->Label(-image => $p, -border => 0)->pack;
    for (0..99) {
        $p->put("#000000", -to => $_,$_);
        $p->put("#000000", -to => 100-$_,$_);
    }
    MainLoop;