[R-sig-Geo] writeRaster and RGB vs Gray

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Fri Apr 26 21:53:01 CEST 2013

On Fri, Apr 26, 2013 at 8:25 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:
> Here's a little example script using functions from the raster package,
> r1 <- r2 <- r3 <- raster(ncol=25,nrow=25,
>      xmn=0, ymn=0, xmx=1, ymx=1)
> r1[] <- round(runif(25^2,0,255))
> r2[] <- round(runif(25^2,0,255))
> r3[] <- round(runif(25^2,0,255))
> s <- stack(r1,r2,r3)
> plotRGB(s)
> writeRaster(s,'s.tif', format='GTiff', overwrite=TRUE,
>             bandorder='BSQ')
> When I open the file s.tif in QGIS, it's fine; I see the same image as in
> R.
> But the Mac platform's default viewer, Preview.app, thinks the file's
> color model is "Gray", rather than "RGB", and it does not render properly.
> Preview does recognize, for example, a jpeg photo as having an RGB color
> model.
> (Adobe Reader (for Mac) wouldn't open it at all.)
> Is there some better set of arguments to give writeRaster that will
> somehow make the file more broadly recognizable as RGB?

 Hmmm. I think QGIS just makes a guess that any 3-band image is R,G
and B. But there must be some tag in a tiff file that specifies this

  If I create a tif as above, and also create one with gimp, then I
can run 'tiffinfo' on them:

 The R one has "Photometric Interpretation: min-is-black", whereas the
Gimp one has "RGB Color". The problem is now how to change that tag in
the R output tiff!

 This can be done on the command line:

tiffset -s 262 2 s.tif

 where 262 is the id of the photometric interpretation tag and 2 means
'rgb': http://www.awaresystems.be/imaging/tiff/tifftags/photometricinterpretation.html

 but I can't see how to do this in writeRaster...

 ah ha.

writeRaster([all that other stuff], options="PHOTOMETRIC=RGB")

got from the list of gdal driver options: http://www.gdal.org/frmt_gtiff.html



More information about the R-sig-Geo mailing list