Version 21 of Image manipulation without Tk

Updated 2003-09-12 14:12:33

[Explain motivation (cron, possible sneer at Java, portability, ...]

LV Can someone go into the technical details of what parts of Tk require a display and what parts do not?

For instance, if one were to write a C app which created a canvas, placed things onto the canvas, configured various colors, text, etc., then commanded the canvas to create PostScript, would a display be necessary for any of this?

According to Robert Heller in a comp.lang.tcl posting, the canvas postscript generating code uses various bits and pieces of Xlib, relating to things like colors, fonts, and conversions of 'image' type data to something that can be converted to postscript.


[Explain alternatives:

  #! /usr/local/bin/tclsh8.3
  puts "Content-Type: image/x-portable-graymap"
  puts ""
  puts "P2"                          ;# ASCII PGM "magic number"
  puts "10 10"                       ;# width and height
  puts "18"                          ;# Max grey value
  for {set i 0} {$i<10} {incr i} {
     set line ""
     for {set j 0} {$j<10} {incr j} {
        lappend line [expr {$i+$j}]
     }
     puts $line
  }
  exit

AM Here is a little variation on this - it produces a PPM file (type "P6"):

 # Create a simple PPM file (type: P6)
 #
 global width
 global height
 set width  200
 set height 200

 set total_length [expr {$width*$height*3}]

 set image [binary format "a$total_length" ""]

 proc setpixel {imageName i j r g b} {
    upvar $imageName image
    global width
    global height

    set posb [expr {3*$width*$j+3*$i}]
    set pose [expr {3*$width*$j+3*$i+2}]
    set image [string replace $image $posb $pose [format %s%s%s $r $g $b]]
 }

 for { set i 0 } { $i < $width } { incr i } {
    setpixel image $i $i \uff \uff \uff
 } 
 for { set i 0 } { $i < $width } { incr i } {
    setpixel image $i 10 \uff \u0 \u0
 }

 set outfile [open "image.ppm" "w"] 
 puts $outfile "P6"
 puts $outfile $width
 puts $outfile $height
 puts $outfile "255 $image"
 close $outfile

A small and incomplete TGA image reader which reads a file into a format that can then later be fairly easily manipulated without the requirement of Tk. The format is compatible with one of the accepted (albeit undocumented) formats for Tk photos.

See: TGA image reader.


See also "strimj - string image routines" and "barchart". GIF has code snippets for writing image files in pure Tcl.


[Do something about Tk's "-nodisplay" ...]


DKF: Another way would be to split the image master stuff out of Tk into its own package which could be loaded from Tcl [L5 ]. Note that the image display code would have to remain in Tk, but that's quite reasonable IMHO.

12 Sept 2003 Mike Tuxford: Now this is exactly what I'd like to see but is far beyond my abilities to accomplish, and I don't feel particularly comfortable making a call for others to do it, because it's something I am unqualified and unable to contribute to.


Arts and crafts of Tcl-Tk programming - Category Graphics