Skip to content
Prev 76021 / 398502 Next

Using tcltk to display jpeg images

Jonathan Williams wrote:
No problems. Indeed, you have to install BINARIES of the img tcl 
package. One solution is to install a Tcl/Tk distribution that includes 
already that package. ActiveTcl is the one that comes to mind 
immediatelly. Here is the procedure to use it:

- Go to http://www.activestate.com/Products/ActiveTcl/, then 'Free 
download', then download ActiveTcl 8.4.11.0 for Windows, which contains 
Img 1.3. Do NOT download ActiveTcl 8.5.XXX: it is not compatible with R!

- Install it, let's say in 'c:\Tcl'

- Create a copy of the shortcut that starts R. Rename it (for instance R 
2.1.1 - ActiveTcl).

- Right click on this shortcut and select 'Properties' in the context 
menu. Then change taget from:
"C:\Program Files\R\rw2011\bin\Rgui.exe"
to:
"C:\Program Files\R\rw2011\bin\Rgui.exe" MY_TCLTK=c:/Tcl

- Start R from this shortcut.

- Load the tcltk package and create a Tk window:
 > library(tcltk)
 > tt <- tktoplevel()

You know that you are using ActiveTcl, and not the Tcl package provided 
with R because the icon of that window is changed from a red "Tk" to the 
  brown plume that is the icon of ActiveState.

- At this point, the Img Tcl package is available to you from R. The 
rest is taken from 
http://bioinf.wehi.edu.au/~wettenhall/RTclTkExamples/showImage.html:
 > R.base.dir <- system.file()
 > setwd(paste(R.base.dir,"/../../doc/html",sep=""))
 > require(tcltk)
Loading required package: tcltk
Loading Tcl/Tk interface ... done
[1] TRUE
 > tt <- tktoplevel()
 > tclRequire("Img")
<Tcl> 1.3
 > image1 <- tclVar()
 > tkcmd("image","create","photo",image1,file="logo.jpg")
<Tcl> ::RTcl1
 > imgAsLabel <- tklabel(tt,image=image1,bg="white")
 > tkpack(imgAsLabel)
<Tcl>

You should read also the R for Windows FAQ 3.6.

Finally, when it works, there is another (and perhaps simpler) way to 
load the Img tcl package, using the Tcl version distributed with R. You 
just have to specify: addTclPath("c:/tcl/lib"). So, you can start R 
normally (using the default Tcl distributed with it), and do:
 > library(tcltk)
Loading Tcl/Tk interface ... done
 > addTclPath("c:/xtra/tcl/lib") # This is how you gain access to the 
ActiveTcl library!
 > R.base.dir <- system.file()
 > setwd(paste(R.base.dir,"/../../doc/html",sep=""))
 > tt <- tktoplevel()
 > tclRequire("Img")
<Tcl> 1.3
 > image1 <- tclVar()
 > tkcmd("image","create","photo",image1,file="logo.jpg")
<Tcl> ::RTcl1
 > imgAsLabel <- tklabel(tt,image=image1,bg="white")
 > tkpack(imgAsLabel)
<Tcl>

Note that you can relocate the /lib subdir of ActiveTcl, or only 
/lib/Img1.3 and use AddTclPath() to access it at the new location. This 
way, you don't have to install ActiveTcl on all the computers where you 
want to use Img. You just need to copy /lib/Img1.3 there and use 
AddTclPath(). However, take care of the ActiveState license that 
constrains you to download their software from their web site and to 
register. So, you can only use this method for multiple installations on 
your own computers... but not for redistribution!

Best,

Philippe Grosjean

..............................................<??}))><........
  ) ) ) ) )
( ( ( ( (    Prof. Philippe Grosjean
  ) ) ) ) )
( ( ( ( (    Numerical Ecology of Aquatic Systems
  ) ) ) ) )   Mons-Hainaut University, Pentagone (3D08)
( ( ( ( (    Academie Universitaire Wallonie-Bruxelles
  ) ) ) ) )   8, av du Champ de Mars, 7000 Mons, Belgium
( ( ( ( (
  ) ) ) ) )   phone: + 32.65.37.34.97, fax: + 32.65.37.30.54
( ( ( ( (    email: Philippe.Grosjean at umh.ac.be
  ) ) ) ) )
( ( ( ( (    web:   http://www.umh.ac.be/~econum
  ) ) ) ) )          http://www.sciviews.org
( ( ( ( (
..............................................................