Version 18 of image

Updated 2003-09-18 17:35:43

image create type ?name? ?option value ...?

image delete ?name name ...?

image height name

image inuse name

image names

image type name

image types

image width name


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

Tk images fall in two categories: bitmap and photo.

[Insert information about Tk's image command, the practical uses, and the little things that can get you, here]

One warning- be very careful what names you use for the image command. In particular, it is not advised that user input be used. This is because when Tk's image command creates an image, any proc/command formerly using that name is replaced. This could be frustrating or even disasterous if a user provided a name such as set or interp, etc.


The Tk image command provides support for a limited number of formats. TkImg (aka 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 [insert details here ... ] and [...].


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 ;-)


See also:


Category Command - Tk syntax help - Arts and Crafts of Tcl-Tk Programming - Category Graphics