I'm wondering if there's an easy way to write PNG files from a matrix?

I've got an NxM matrix of values, z, and I want to produce an NxM
pixel PNG file. I have a function pfunc(.) that converts values to
colours.

So currently I do this:

  library(rtiff)
  library(rgdal)
  z = matrix(runif(100),10,10)
  pfunc=colorRamp(c("blue","yellow"))

  tiffPath = "foo.tif"
  pngPath = "foo.png"

# get the rgb colours:
  rgb = pfunc(t(z)[ncol(z):1,])

# as a pixmapRGB
  rgbpixmap = pixmapRGB(c(rgb[,1],rgb[,2],rgb[,3]),ncol(z),nrow(z))

# save as a tiff (require(rtiff) package)
  writeTiff(rgbpixmap,tiffPath)

# now read in using rgdal and copy, since GDAL PNG driver doesn't have
a 'create' call:
  xp=GDAL.open(tiffPath)
  xx=copyDataset(xp,driver="PNG")
  saveDataset(xx,pngPath)
  GDAL.close(xx)
  GDAL.close(xp)

# we don't need this:
  file.remove(tiffPath)

But it seems a bit kludgy. I know its not really a 'geo' question, but
it seems that rgdal might have a better way to do this that I've not
hit on yet. Any ideas (short of system("convert foo.tif foo.png")). Or
is my solution as elegant as it can be?

Barry

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@stat.math.ethz.ch
https://stat.ethz.ch/mailman/listinfo/r-sig-geo

Reply via email to