1

I have a tried to convert a RasterLayer 16-bit into a RasterLayer 8-bit in R, but I did not have success. Any ideas ?

Thanks!

Mercel Santos
  • 27
  • 2
  • 7
  • 2
    Help us help you by providing a [minimal reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your problem. – eipi10 Aug 11 '15 at 23:29
  • 2
    What did you try? You can do `writeRaster(x, filename, datatype='INT1U')` – Robert Hijmans Aug 12 '15 at 17:14

1 Answers1

3

If you want to convert your RasterLayer from 16-bit to 8-bit you need to stretch your values into the 8-bit interval (0-255 unsigned) first. Then you can save it as an 8bit image:

#sample raster
library(raster)
ras16b <- raster(x=matrix(as.integer(rnorm(180*180,1000,50)),180,180))


#convert to 0-255 using the calc. function and basic raster algebra
ras8b <- calc(ras16b, fun=function(x){((x - min(x)) * 255)/(max(x)- min(x)) + 0})

#export 8b raster
writeRaster(ras8b, '/bla/bla/ras8b.tif', datatype='INT1U')

You can find more information on how to normalize values into the 0-255 interval here

Community
  • 1
  • 1
maRtin
  • 6,336
  • 11
  • 43
  • 66