Version 7 of Creating image photo data from GIF files

Updated 2003-07-05 14:47:59

Ever want to embed images directly into your Tcl code, eliminating the need to carry a bunch of GIF files along with your application? Jeff Godfrey writes on comp.lang.tcl, "Yes, this can be done, and quite simply too..."

1. First, you must convert your gif images to base64 encoded data. You can do this with using the base64 package included in tcllib:

 package require base64
 set fileID [open "mypic.gif" RDONLY]
 fconfigure $fileID -translation binary
 set rawData [read $fileID]
 close $fileID
 set encodedData [base64::encode $rawData]

(Using the above, it's quite simple to write a program that will encode a group of gif files...) The "encodedData" variable will now contain the image data encoded as a string that can stored in a variable in your script.

2. Now, to use the encoded image, just convert it to a "photo" and pass it to the appropriate widget:

 set imageData "<base64 encoded data string>"
 set myPic [image create photo -data $imageData]
 label .l1 -image $myPic
 pack .l1

So, basically:

1. Encode all of your images and store the encoded strings in your script

2. Convert the encoded strings to photo images and use as needed (as above).

3. Season to taste... You can find some good seasonings at Tk image Dos and Don'ts


If you have groups of images to convert, you may try the following script. It creates a package with the encoded image data available as procs.

 # ImgToPkg.tcl
 # Tcl package generator for image files.
 #
 # Usage: tclsh ImgToPkg.tcl > pkgIndex.tcl
 #
 # Test the generated package with code like the following with wish:
 # package require myImgPkg
 # label .l
 # pack  .l
 # set img [image create photo -data [::myImgNamespace::imgName1]]
 # .l configure -image $img
 #
 # Paul Obermeier, 2002.

 package require base64

 proc EncodeImgData { fileName } {
     set fp [open $fileName "r"]
     fconfigure $fp -translation binary
     set rawData [read $fp [file size $fileName]]
     close $fp
     return [base64::encode $rawData]
 }

 proc SaveImgPackage { fileList outFile packageName namespaceName versionNum } {
     set retVal [catch {open $outFile w} fp]
     if { $retVal != 0 } {
         error "Could not open output file $outFile"
     }
     fconfigure $fp -translation binary

     # Write package header.
     puts $fp "package provide $packageName $versionNum"
     puts $fp ""

     # Write namespace header.
     puts $fp "namespace eval ::$namespaceName {"
     foreach fileName $fileList {
         set imgName [file rootname [file tail $fileName]]
         if { [file readable $fileName] } {
             puts $fp "    namespace export $imgName"
         }
     }
     puts $fp "}"
     puts $fp ""

     # Generate namespace procedures.
     foreach fileName $fileList {
         set imgName  [file rootname [file tail $fileName]]
         set procName [format "%s::%s" $namespaceName $imgName]

        puts $fp "proc ::$procName \{\} \{"
        puts $fp "return \{"
        puts $fp "[EncodeImgData $fileName]"
        puts $fp "\}"
        puts $fp "\} \; \# End of proc $procName"
        puts $fp ""
     }
     close $fp
     puts "# Put the following line into a pkgIndex.tcl file:"
     puts "package ifneeded $packageName $versionNum \"source \[file join \$dir $outFile\]\""
 }

 # Test code. Start with tclsh as noted above.

 set imgFileList [glob *.gif]
 SaveImgPackage $imgFileList "myImgPkg.tcl" "myImgPkg" "myImgNamespace" "1.2.3"
 exit 0

Category Graphics