Version 32 of Img

Updated 2004-08-30 14:50:09 by MG

The venerable 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 ] (Note: I was unable to download source from this page on 23Aug2004. If this happens to you, use the sourceforge link below. Please remove this note if the problem is fixed. --SAH)

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

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


The latest version is now 1.3. Is it a stable version?

DESCRIPTION

Img is a Tk extension, adding support for many other Image formats, as listed below. The individual formats are described in more detail on their own pages.

  • bmp: Windows bitmap format. See img-bmp .
  • gif: The venerable graphics interchange format (with transparency, but without LZW. The latter due to patent issues). See img-gif .
  • ico: Windows icon files. See img-ico .
  • jpeg: The format for lossy compressed still pictures specified by the Joint Picture Experts Group. See img-jpeg .
  • pcx: Paintbrush image format. See img-pcx .
  • pixmap: While the other formats are handlers for the Tk photo image type this is a new image type for Tk. See img-pixmap .
  • png: Portable Network Graphics, successor to GIF. See img-png .
  • ppm: Portable Pixmaps. See img-ppm .
  • ps: Postscript and PDF (Portable document format). Requires an external application, ghostview, for its operation. See img-ps .
  • sgi: Native SGI format. See img-sgi .
  • sun: Sun raster images. See img-sun .
  • tga: Targa files. See img-tga .
  • tiff: Tagged Interchange File Format. See img-tiff .
  • window: ??? (not a file but window snapshot) See img-window .
  • xbm: X Bitmaps. See img-xbm .
  • xpm: X Pixmaps. See img-xpm .

This version only works with Tcl/Tk 8.2 or higher. If you are using Tk 8.1 or lower please stick with Img 1.2.4 (which is still available). And if you are using Tk4.2 or lower, please stick with Img 1.1.4 (which is also still available).


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
        }
    }

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.


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

MG Sure..

 package require Img
 image create photo myJpeg -file "image.jpeg"
 pack [label .l -image myJpeg] ;# to show it works ok

It's as simple as that :) As for writing images (after you've executed the above code)...

 myJpeg write "image.png" -format PNG

Where can one get freely distributable win32 binaries?

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

A comparison with TclMagick would also be useful.


[ Category Package | Category Graphics ]