Paul.Rustomji at csiro.au
2009-Jul-15 01:49 UTC
[R] Read PNG file and display with more than 256 colors RGDAL
Hello list I am trying to use a Googlemaps tile (png file, 640 X 640 px tile) as a background to a plot and have been using the rgdal library to read in the PNG file (modified from code in the RGoogleMaps package). This works OK. My problem is is that the SGDF2PCT function in rgdal seems to be limited to 256 colors and this is introducing (I think) some degradation to the image which appears on the output graph as speckling. Is there a way around this? I have been reading about the EBImage package but it (a) seemed to have too many dependencies on other software for my purposes and (b) wouldn't load properly in any case on my machine (couldn't find dll's that it was looking for....) Ideally I would like to read in the png image and display in its original form as a plot background, hopefully with all its colors represented rather than being converted to a 256 color scale. A code example is below: png(file="file.png", 4000,4000) par(mar=c(0,0,0,0)) myTile <- readGDAL("basemap.png",silent=TRUE); #basemap.png is a PNG file returned from googlemaps myTile at data <- myTile at data[,1:3] col <- SGDF2PCT(myTile,ncolors=256) ## myTile is a spatialGridDataFrame with 3 bands myTile$ind <- col$idx ## add the colour index to the data frame myTile <- as.image.SpatialGridDataFrame(myTile["ind"],1,2)$z; attr(myTile, "COL") <- col$ct; attr(myTile, "type") <- "rgb"; myTile <- list(lat.center, lon.center,NA, myTile) image(z=myTile[[4]], col = attr(myTile[[4]], "COL")) dev.off()> sessionInfo()R version 2.9.1 (2009-06-26) i386-pc-mingw32 locale: LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods base other attached packages: [1] abind_1.1-0 rgdal_0.6-9 sp_0.9-37 gridBase_0.4-3 loaded via a namespace (and not attached): [1] lattice_0.17-25 tools_2.9.1 Paul Rustomji
Roger Bivand
2009-Jul-15 11:51 UTC
[R] Re ad PNG file and display with more than 256 colors RGDAL
If you read the help page for the image method you are actually using: ?image.SpatialGridDataFrame you see that it supports red=, green=, and blue= arguments. This suggests that you could try: myTile <- readGDAL("basemap.png",silent=TRUE) class(myTile) summary(myTile) # assuming the positions are 1, 2, 3 image(myTile, red=1, green=2, blue=3) On the other hand, the "colours" in these images are arbitrary manipulations of the sensors, and have already been warped and resampled, so decimating the colour representation isn't inappropriate, and 256 is not a small number. Roger Bivand PS. Detailed rgdal questions like this may get a faster response on R-sig-geo Paul.Rustomji wrote:> > Hello list > > I am trying to use a Googlemaps tile (png file, 640 X 640 px tile) as a > background to a plot and have been using the rgdal library to read in the > PNG file (modified from code in the RGoogleMaps package). This works OK. > My problem is is that the SGDF2PCT function in rgdal seems to be limited > to 256 colors and this is introducing (I think) some degradation to the > image which appears on the output graph as speckling. > > Is there a way around this? I have been reading about the EBImage package > but it (a) seemed to have too many dependencies on other software for my > purposes and (b) wouldn't load properly in any case on my machine > (couldn't find dll's that it was looking for....) > > > Ideally I would like to read in the png image and display in its original > form as a plot background, hopefully with all its colors represented > rather than being converted to a 256 color scale. > > > A code example is below: > > png(file="file.png", 4000,4000) > par(mar=c(0,0,0,0)) > myTile <- readGDAL("basemap.png",silent=TRUE); #basemap.png is a PNG > file returned from googlemaps > myTile at data <- myTile at data[,1:3] > col <- SGDF2PCT(myTile,ncolors=256) ## myTile is a spatialGridDataFrame > with 3 bands > myTile$ind <- col$idx ## add the colour index to the data frame > myTile <- as.image.SpatialGridDataFrame(myTile["ind"],1,2)$z; > attr(myTile, "COL") <- col$ct; > attr(myTile, "type") <- "rgb"; > myTile <- list(lat.center, lon.center,NA, myTile) > > image(z=myTile[[4]], col = attr(myTile[[4]], "COL")) > > dev.off() > >> sessionInfo() > R version 2.9.1 (2009-06-26) > i386-pc-mingw32 > > locale: > LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252 > > attached base packages: > [1] grid stats graphics grDevices utils datasets methods > base > > other attached packages: > [1] abind_1.1-0 rgdal_0.6-9 sp_0.9-37 gridBase_0.4-3 > > loaded via a namespace (and not attached): > [1] lattice_0.17-25 tools_2.9.1 > > > Paul Rustomji > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Read-PNG-file-and-display-with-more-than-256-colors-RGDAL-tp24490663p24496356.html Sent from the R help mailing list archive at Nabble.com.