Version 39 of Img

Updated 2004-10-15 12:31:40 by lwv

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 original Img homepage can be found here: [L1 ], however the code there is the older 1.2.4 version. Currently, the official Img source code (version 1.3 or newer) is now maintained in the SourceForge http://sourceforge.net/projects/tkimg/ project.

(Note: I was unable to download source from the original Img page on 23Aug2004. If this happens to you, use the sourceforge link . Please remove this note if the problem is fixed. --SAH)

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?

MG Img is included in the ActiveTcl binaries. I don't know where you can find a binary of Img on its own, though...

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

A comparison with TclMagick would also be useful.


sheila 2004/10/14

Which Windows BMP formats does Img support? I am sorry I was not able to find this information. I've been playing around with it, and I was able to handle 256 color BMPs, but not 24 bit BMPs. However, when I got back an error about the 24 bit BMP, the error said that Img did not support 32 bit BMPs, not 24.

Also, what is the default format that is returned when one uses the

 $image_256.bmp data

as opposed to specifying -format bmp? I've played around with that, and I get something like a list of lists, and the elements of the list are #rrggbb values. When I use -format bmp I am not sure what format I get. I'm guessing base64 encoded. You can go from this format to hex in order to manipulate bits in hex maybe

 package require base65

this is a row from the first line of a dump from $image data -format bmp.

 % set row
 Qk322AEAAAAAADYEAAAoAAAAkAEAACwBAAABAAgAAAAAAMDUAQBtCwAAbQsAAAABAAAAAQAA

 % set d64 [base64::decode $row]
 BM and binary formated data

 % binary scan $d64 H* hex
 1
 % set hex
 424df6d80100000000003604000028000000900100002c0100000100080000000000c0d401006d0b00006d0b00000001000000010000

Having the rgb data already conveniently handle from the #rrggbb elements is nice. Is there a way to convert that back to a format that can be used to create new bmp images?


[ Category Package | Category Graphics ]