[R] Raster images and saving with original pixel dimensions in tiff, jpeg, or png perferablly.

Hans Thompson hans.thompson1 at gmail.com
Sat Mar 2 23:03:32 CET 2013


Hello R-Help,

I want to be able to read in a raster image, plot it with grid.raster
or rasterImage and save the image with one pixel per a pixel element
from my array. Saved preferably in a common image format.

The real goal of my question is to eventually read in images with text
on them, manipulate them with my controlled functions, save them
without changing the image dimensions, and perform OCR outside R.

I have read The R Journal article on raster images
(http://journal.r-project.org/archive/2011-1/RJournal_2011-1_Murrell.pdf)
and have experimented with some of the par() variables with no
success.  There are some arguments in gird.raster that elude me
currently on how to use them.  These include, vjust, hjust,
default.units, gp and vp.  Perhaps these are what I need to use but I
am getting nowhere without more documentation.

Here is the basic code I've used to experiment with making this work.
The end result I'm hoping for would be an image exactly like the one
I've read in (Rlogo.jpg for this small example).  I turned off
dev.copy lines because I'm not sure on the guidelines for file writing
functions.

Thanks,
Hans Thompson

Forgive me If I made any mistakes following posting guidelines.

####START#####
library(jpeg)
library(grid)

img <- readJPEG(system.file("img", "Rlogo.jpg", package="jpeg"))
grid.raster(img, interpolate =F)
#turnon next line for output. Don't want to accidentally write any
unwanted files.
#dev.copy(tiff, "outputimage.tiff")
dev.off()

#or using package = "graphics" but not perfered.

library(graphics)

plot( c(0, dim(img)[2]), c(0, dim(img)[1]), type = "n", xlab = "", ylab = "")
rasterImage(img, 0, 0, dim(img)[2], dim(img)[1])
#dev.copy(tiff, "outputfile2.tiff")
dev.off()

##STOP##



More information about the R-help mailing list