Version 23 of Img

Updated 2003-09-22 16:23:46

The IMG package by Jan Nijtmans provides the handling of several image formats beyond the standard formats in Tk. The formats supported by Img 1.2.4 (the latest stable version) are:

The Img homepage can be found here: [L1 ]

Sources available at SourceForge: http://sourceforge.net/projects/tkimg/

Img is bundled as part of ActiveState's ActiveTcl binary distributions.


Can anyone provide examples of how to use this package with Tk to display some of the above image types?


LV Does Img work with Tk 8.4 ?

AK It should. I remember that I did an experimental build of ActiveTcl against 8.4cvs and the compilation went through without a hitch.

Note: This is about the Img sources in the CVS. Img 1.2.4 most definitely does not compile against 8.4 because of changes in some public API functions of Tk regarding Img handling.


I often have a need to convert from one graphics format to another. As long as I have ActiveTcl around, the chore is easy; all I need is a script on the order of

    package require Img

    cd /temp

    foreach file [glob *.bmp] {
        set root [file rootname $file]
        set image [image create photo -file $file]
        foreach {format suffix} {JPEG jpg GIF gif PNG png} {
            $image write $root.$suffix -format $format
        }
    }

['Would be valuable to compare its capabilities with Xbit's.]


Capture a window into an image (screenshot) (posted by Mark G. Saye in the comp.lang.tcl newsgroup):

 proc capture {W format file} {
   set image [image create photo -format window -data $W]
   $image write -format $format $file
   puts "capture -> '$file' ([file size $file] bytes)"
   image delete $image
 }

   package require -exact Img 1.2.4

   set top .t
   toplevel $top
   frame $top.f
   pack  $top.f -fill both -expand 1

   label $top.f.hello -text "Hello World"
   pack  $top.f.hello -s top -e 0 -f none -padx 10 -pady 10
   update
   bind $top <Key-x> [list capture $top gif capture.gif]

Saves a white image only, however ;-(. Paul Obermeier has this:

 proc canvas2Photo { canvId } {
    # The following line grabs the contents of the canvas canvId into photo image ph.
    set retVal [catch {image create photo -format window -data $canvId} ph]
    if { $retVal != 0 } {
       puts "\n\tFATAL ERROR: Cannot create photo from canvas window"
       exit 1
    } 
    return $ph
 }

RS experimented with this and found that it can convert canvas, text and listbox widgets, but not a compound toplevel. Hence, the name might better be widget2photo... or, by using default error handlers, be simplified to an interp alias:

 interp alias {} capture {} image create photo -format window -data

David Easton: 17 Jun 2003 Capture a window into an image extends this to work for a whole toplevel window complete with sub-widgets.


The combination of photo image zooming and the Img extension let us code A little magnifying glass in just a few lines.


[ Category Package | Category Graphics ]