4

I'm trying to create a 8 bit GeoTIFF with this code.

Is there a way to use unsigned 8 bit? I only found the GDT_UInt16?

import numpy as np
from osgeo import ogr, osr, gdal
from osgeo.gdalconst import *

dataset = gdal.Open('test.tif')
driver = dataset.GetDriver()
driver.Create("out.tiff", cols, rows, 1, GDT_UInt16) # <- here

out_band = out_dataset.GetRasterBand(1)
out_data = np.zeros((rows,cols), np.int16)
out_band.WriteArray(out_data, 0, 0)
out_band.FlushCache()
out_dataset.SetGeoTransform(dataset.GetGeoTransform())
out_dataset.SetProjection(dataset.GetProjection())

2
  • 1
    From gdal.org/frmt_gtiff.html: Currently band types of Byte, UInt16, Int16, UInt32, Int32, Float32, Float64, CInt16, CInt32, CFloat32 and CFloat64 are supported for reading and writing. Byte seems to be the closest hit.
    – user30184
    Nov 20, 2014 at 9:53
  • Exactly 8 bits are 1 byte. Thank you for the reminder :)
    – gustavgans
    Nov 20, 2014 at 11:00

1 Answer 1

4

The gdal documentation describes the GDT_Byte as an 8 bit unsigned integer (see here). So the correct gdal constant is the GDT_Byte. In your code it would be:

driver.Create("out.tiff", cols, rows, 1, gdal.GDT_Byte) 
1
  • also worth noting here if you've come to this question to find how to assign this in a terminal program, you can use... -ot Byte as opposed to -ot UInt16
    – rickD
    Mar 20, 2021 at 1:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.