Version 13 of image

Updated 2003-09-16 06:27:03

http://www.purl.org/tcl/home/man/tcl8.4/TkCmd/image.htm

Tk images fall in two categories: bitmap and photo.


Img is a widely-used extension that beefs up image's capabilities. Note that

    $image write ...

can write GIF and PPM, but

    $image data ...

cannot.

KBK offers an example which relies on base64 to embed GIFs in scripts:

    package require base64
    $img write myimage.gif -format gif
    set f [open myimage.gif r]
    set data [read $f]
    close $f
    set script [list set img \
        [list image create photo -data [base64::encode $data]]]

Img turns this into a one-liner:

    % package require Img
    1.2.4
    % set i1 [image create photo -file \
        [file join $::tk_library demos images earthris.gif]]
    image2
    % set s1 [$i1 data -format gif]; string length $s1
    15192
    % rename $i1 {}
    % set i2 [image create photo -data $s1]
    image3

Img does much beyond this, including ... and ...


See also


I (FW) wrote this code in a couple minutes the other day to occupy myself, which generates a 500x500 image of randomly-colored pixels and displays it in a window. I think this summarizes Tcl's image generation capabilities well (and the images it makes are pretty mesmerising, too). Might as well put it up here:

 proc generate_data {} {
   set data [list]
   for {set x 0} {$x < 500} {incr x} {
     set row [list]
     for {set y 0} {$y < 500} {incr y} {
       lappend row [format "#%02x%02x%02x" [random_byte] [random_byte] [random_byte]]
     }
     lappend data $row
   }
   return $data
 }

 proc random_byte {} {
   return [expr {int(rand() * 256)}]
 }

 set image [image create photo] 
 $image put [generate_data]

 label .l -image $image
 pack .l

RS: Cool! In my copy I just added the line

 bind . <Return> {$image put [generate_data]}

to let the CPU work more ;-)


MGS [2003/09/15] - Note that image create will overwrite existing images of the form imagex, where x is an integer:

 % image create photo image1
 image1
 % image create photo
 image1

It appears that Tk uses an internal counter for image names, and does not check if imagex already exists.


Tk syntax help - Arts and crafts of Tcl-Tk programming - Category Command - Category Graphics