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: * [BMP] * [XBM] * [XPM] * [GIF] (with transparency, but without [LZW], due to patent restrictions) * [PNG] * [JPEG] * [TIFF] * [postscript] The original Img homepage can be found here: [http://members1.chello.nl/~j.nijtmans/img.html], 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 [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/18 What is the default format that is returned when one uses the $image_256.bmp data as opposed to specifying ''-format bmp''? Looks like a list of lists, and the elements of the list are #rrggbb values. When I use ''-format bmp'' I'm guessing I get base64 encoded data. 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. (this is just a rendition, since the actual characters aren't printable to this screen) % set row Qk322AEAAAAAADYEAAAoAAAAkAEAACwBAAABAAgAAAAAAMDUAQBtCwAAbQsAAAABAAAAAQAA % set d64 [base64::decode $row] BM and binary formated data % binary scan $d64 H* hex 1 % set hex 424df6d80100000000003604000028000000900100002c0100000100080000000000c0d401006d0b00006d0b00000001000000010000 This example is a Windows BMP 24-bit format. After you get this, you can parse the file to access data of interest. I was curious about how to do a difference operation on two bitmaps, and wanted to xor the rgp data, so I was going to do a parser for the BMP file. I didn't do that after discovering the TclMagick extension for accessing the ImageMagick API (Hmm, I'd like to reduce the footprint of my ImageMagick install). I'm still curious though. Having the rgb data already conveniently returned in #rrggbb elements is nice. Is there a way to convert that back to a format that can be used to create new bmp images automatically, or would I have to write a generator for creating a new BMP file to pass to image create? [AF] - The [tklib] [ico] package will read and write BMPs to/from lists of rgb colors and tk images. ---- [petasis] I have written an improved version of the canvas2Photo procedure. This one is able to capture *all* canvas items in the image, and not the visible part only... proc canvas2Photo {canvas image} { ## Ensure that the window is on top of everything else, so as not to get ## white ranges in the image, due to overlapped portions of the window with ## other windows... raise [winfo toplevel $canvas] update set border [expr {[$canvas cget -borderwidth] + [$canvas cget -highlightthickness]}] set view_height [expr {[winfo height $canvas]-2*$border}] set view_width [expr {[winfo width $canvas]-2*$border}] foreach {x1 y1 x2 y2} [$canvas bbox all] {break} set x1 [expr {int($x1-10)}] set y1 [expr {int($y1-10)}] set x2 [expr {int($x2+10)}] set y2 [expr {int($y2+10)}] set width [expr {$x2-$x1}] set height [expr {$y2-$y1}] image create photo $image \ -height $height -width $width ## Arrange the scrollregion of the canvas to get the whole window visible, ## so as to grab it into an image... set scrollregion [$canvas cget -scrollregion] set xscrollcommand [$canvas cget -xscrollcommand] set yscrollcommand [$canvas cget -yscrollcommand] $canvas configure -xscrollcommand {} $canvas configure -yscrollcommand {} set grabbed_x $x1 set grabbed_y $y1 set image_x 0 set image_y 0 while {$grabbed_y < $y2} { while {$grabbed_x < $x2} { $canvas configure -scrollregion [list $grabbed_x $grabbed_y \ [expr {$grabbed_x + $view_width}] [expr {$grabbed_y + $view_height}]] update ## Take a screenshot of the visible canvas part... image create photo ${image}_tmp \ -format window -data $canvas ## Copy the screenshot to the target image... $image copy ${image}_tmp \ -to $image_x $image_y -from $border $border incr grabbed_x $view_width incr image_x $view_width } set grabbed_x $x1 set image_x 0 incr grabbed_y $view_height incr image_y $view_height } $canvas configure -scrollregion $scrollregion $canvas configure -xscrollcommand $xscrollcommand $canvas configure -yscrollcommand $yscrollcommand return $image } ---- [[ [Category Package] | [Category Graphics] ]]