I am working on dumping raster data from R into PNG files using rasterImage(). I am working with a test matrix from the rasterImage() example and using it to produce a PNG image with the following code: # From the example for rasterImage(). A 3 pixel by 5 pixel b/w checkerboard. testImage <- as.raster(0:1, nrow=3, ncol=5) testImage [,1] [,2] [,3] [,4] [,5] [1,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000" [2,] "#FFFFFF" "#000000" "#FFFFFF" "#000000" "#FFFFFF" [3,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000" png('test.png', width=5, height=3, units='px') # Just want the image, no margins, boarders or other fancy stuff. par(mar = c(0,0,0,0) ) plot.new() plotArea = par('fig') rasterImage(testImage, plotArea[1], plotArea[3], plotArea[2], plotArea[4], interpolate = FALSE ) dev.off() However, using R 2.12.0, 64 bit on Windows 7 I have a strange issue where the image is shifted up by one row and to the left by one row. In other words, the bottom row of pixels is missing along with the right column. The code works as I expect it to on OS X and Debian. Am I misusing the plotting commands in some way or should I submit an off-by-one bugreport to Bugzilla? Any suggestions or comments are most welcome. -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://r.789695.n4.nabble.com/Incorrect-positioning-of-raster-images-on-Windows-tp2999649p2999649.html Sent from the R help mailing list archive at Nabble.com.
Michael Sumner
2010-Oct-18 08:18 UTC
[R] Incorrect positioning of raster images on Windows
I think there's something about the "discrete cell" versus "centre value" interpretation here, and you are pushing the "pixels" through R's graphics engine as well as whatever the png device has to do. I can't enlighten you about the details of that, but by creating an image file more directly with "pixels as data" you can get the result exactly: test <- matrix(c(0, 255), 3, 5) library(rgdal) ## transpose to get orientation right x <- image2Grid(list(x = 1:ncol(test), y = 1:nrow(test), z = t(test))) writeGDAL(x, "raster.png", driver = "PNG", type = "Byte") On Mon, Oct 18, 2010 at 3:17 PM, Sharpie <chuck@sharpsteen.net> wrote:> > I am working on dumping raster data from R into PNG files using > rasterImage(). I am working with a test matrix from the rasterImage() > example and using it to produce a PNG image with the following code: > > > # From the example for rasterImage(). A 3 pixel by 5 pixel b/w > checkerboard. > testImage <- as.raster(0:1, nrow=3, ncol=5) > > testImage > [,1] [,2] [,3] [,4] [,5] > [1,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000" > [2,] "#FFFFFF" "#000000" "#FFFFFF" "#000000" "#FFFFFF" > [3,] "#000000" "#FFFFFF" "#000000" "#FFFFFF" "#000000" > > png('test.png', width=5, height=3, units='px') > > # Just want the image, no margins, boarders or other fancy stuff. > par(mar = c(0,0,0,0) ) > plot.new() > plotArea = par('fig') > > rasterImage(testImage, plotArea[1], plotArea[3], > plotArea[2], plotArea[4], interpolate = FALSE ) > > dev.off() > > > However, using R 2.12.0, 64 bit on Windows 7 I have a strange issue where > the image is shifted up by one row and to the left by one row. In other > words, the bottom row of pixels is missing along with the right column. > The > code works as I expect it to on OS X and Debian. > > > Am I misusing the plotting commands in some way or should I submit an > off-by-one bugreport to Bugzilla? > > Any suggestions or comments are most welcome. > > -Charlie > > ----- > Charlie Sharpsteen > Undergraduate-- Environmental Resources Engineering > Humboldt State University > -- > View this message in context: > http://r.789695.n4.nabble.com/Incorrect-positioning-of-raster-images-on-Windows-tp2999649p2999649.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Michael Sumner Institute for Marine and Antarctic Studies, University of Tasmania Hobart, Australia e-mail: mdsumner@gmail.com [[alternative HTML version deleted]]
Possibly Parallel Threads
- Incorrect positioning of raster images on Windows
- Raster images and saving with original pixel dimensions in tiff, jpeg, or png perferablly.
- spatstat => owin + image
- strange interaction between rasterImage and Grid graphics
- Re: [libvirt-users] virt-sparsify changing the apparent-size of files