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

Paul Murrell paul at stat.auckland.ac.nz
Mon Mar 4 20:32:31 CET 2013


Hi

It sounds like you just want to control the size of the external raster 
image that you are creating.  For example ...

library(grid)

im <- matrix(0:1, ncol=3, nrow=3)

# grid.newpage()
grid.raster(im, interpolate=FALSE)

png("ident.png", 3, 3)
grid.raster(im, interpolate=FALSE)
dev.off()

Does that help?

Paul

On 03/03/13 11:03, Hans Thompson wrote:
> 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##
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list