[R] gif, jpeg and png image files reader

Jason Turner jasont at indigoindustrial.co.nz
Tue Mar 12 22:17:22 CET 2002


One quick point ...
On Tue, Mar 12, 2002 at 09:39:28AM -0800, Jonathan Q. Li wrote:
> Yah, but what if I need to write a package in R that reads these images?
> It's
> too much manual trouble to use outside software. It's especially infeasible
> if
> these become a routine task with many images.
...
> -----Original Message-----
> From: Roger Peng [mailto:rpeng at stat.ucla.edu]
> Sent: Monday, March 11, 2002 5:43 PM
> To: jonathan_li at agilent.com
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] gif, jpeg and png image files reader
> 
> 
> The pixmap package has functions for reading pnm (portable anymap) files,
> but you still need an outside software to convert (like ImageMagick).   
> -------

The ImageMagick suite Roger Peng was refering to can be run from the
command line, so automating this with a wrapper in R isn't too hard
(though tricky to do in a portable way).   There are also C, C++, Perl,
and Java interfaces to ImageMagick, so you could do it the more
robust and secure way (what's below is quick and dirty, though 
it works for me).

You can get ImageMagick from http://www.imagemagick.org/

This works so far on my Linux box.  I've tried it for gif, jpg, png, 
xpm, and postscript images.  Convert will handle many more formats too.
Just give it the filename of the image, optionally the path to the 
"convert" command, and any additional arguments to "convert" - rarely 
needed, as convert is *extremely* clever at working out image formats.

###BARELY TESTED CODE!!!  PROBABLY HAS BUGS!!!
## requires the "convert" program from ImageMagick
##to be available to the user.  ImageMagick can be
##obtained from http://www.imagemagick.org/

read.image <- function(file, imdir="/usr/X11R6/bin/",...) { 

	if(!require(pixmap)) stop("library pixmap required and not found\n")
        
        newfile <- tempfile() ##note the race condition on this line and next
        cmd <- paste(sep="",imdir,"convert ", ... ," ",file," pnm:",newfile)

        retval <- system(cmd)
        if(retval != 0) { 
                errmsg <- paste("system command",cmd,"failed with error code",
                        retval,"\n")
                stop(errmsg)
        }
        p1 <- read.pnm(newfile)
	unlink(newfile)
        p1
} 

Also, IIRC, R has specific instructions on startup on how to deal with
things if the above isn't enough....  lesse here....  ah, yes:
"R is a collaborative project with many contributors."  
Or, to (mis?) quote Ghandi: "Be the change you seek in the world."

Cheers

Jason
-- 
Indigo Industrial Controls Ltd.
64-21-343-545
jasont at indigoindustrial.co.nz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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