Version 16 of Img

Updated 2002-11-12 21:38:32

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:

  • BMP
  • XBM
  • XPM
  • GIF (with transparency, but without LZW, due to patent restrictions)
  • PNG
  • JPEG
  • TIFF
  • postscript

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.


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.


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

[ Category Package | Category Graphics ]