[R] gif, jpeg and png image files reader AND tcltk image

jonathan_li@agilent.com jonathan_li at agilent.com
Thu Mar 14 04:27:12 CET 2002


Hi all,

Roger Peng and Jason Turner's suggestion with ImageMagick seem to be the
simplest "dirty" way
to get the problem solved. 

But I ran into yet another interesting but quite round-about way to solve
the problem (partially). Through tcltk package, one can read in the gif
image with

> x <- tkcmd("image", "create", "photo", file=mypic.gif)
> x.data <- tkcmd(x, "data")
gives you the data for the image, but is in a format internal to tcltk. But
then it turns out
that the format is very straightforward to understand: the enclosed simple
function decodes
the format and returns a matrix for the data,
> x.data.ascii <- tk2ascii(x.data)
Then we are done! (This function currently only deals with red color
channel, but expansion to other 2 are very straightforward.) 

On a related note, I have earlier posted a message to ask about whether one
can directly paint an image matrix to a tcltk canvas, the answer is no. One
has to create a pnm file from the image matrix then read in file and then
paint it. When the image is large, the speed of reading and writing disk can
be annoyingly slow. 

Now I begin to believe that it's possible to use 
> x.data <- ascii2tk(imagematrix)
> tkcmd(x, "put", x.data)
where ascii2tk would encode the ascii data into tk internal format for an
image. 

Agustine Lobo offers to provide some functions on display matrices as
images. I would be quite interested in knowing them! On the other hand,
tcltk images provide strong capability for interactivity: cool things such
as point your mouse on the image to get the gray level reading, clipping an
area of the image using mouse etc. Auguably, all these cool image processing
things can be done in other tools such as GIMP. But I would also argue that
to be able to do them in R would increase the productivity of image
processing tasks considerably once the foundation of these tools are laid
out. 
(I have made a few functions that do these cool things entirely in R with
help of tcltk, but they are buggy right now). 

Cheers!
Jonathan




tk2ascii <- function(x){
	###########################
	## tk2ascii converts a tk returned image data
	## into a matrix of integers
	##
	## x: is a tk returned string
	## value: is a matrix of integer representing
	## the image
	###########################	

	list1 <- strsplit(x, "} {")[[1]]
	ROWS <- length(list1)
	list1.1 <- strsplit(list1[1], " #")[[1]]
	COLS <- length(list1.1)
	im <- matrix(0, ROWS, COLS)

	for(i in 1:ROWS){
		row <- strsplit(list1[i], " #")[[1]]
		if(i==1){
			row[1] <- substring(row[1], 2)
		}
		if(i==ROWS){
			row[COLS] <- substring(row[COLS],0, 6)	
		}	
		row[1] <- substring(row[1],2)
		for(j in 1:COLS){
			im[i,j] <- as.integer.hex(substr(row[j], 1,2))
		}	
		#im[i,] <- row
	}

	im
}

as.integer.hex <- function(x){
	##############################
	# as.integer.hex converts a string that
	# represents the hexidecimal number
	# into an integer
	#
	#
	# x must be a string with
	# first digit being the high digit. 
	# value: converted integer
	##############################
	
	len <- nchar(x)
	val <- 0
	for(i in 1:len){
		digit <- substr(x, i,i)
		if(is.na(digit.val <- as.integer(digit))){
			if(digit == "a") digit.val <- 10
			if(digit == "b") digit.val <- 11
			if(digit == "c") digit.val <- 12
			if(digit == "d") digit.val <- 13
			if(digit == "e") digit.val <- 14
			if(digit == "f") digit.val <- 15
		}
		val <- val + digit.val* 16^{len-i}
	}
val
}


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list