[gdal-dev] Convert to min containing bit depth?

Idan Miara idan at miara.com
Thu Apr 21 23:18:12 PDT 2022


Computing the min value is also requited if you have negative values and
could also be useful If you wanted to optimize that method further by
utilising the offset parameter (and or the scale, but computing the right
combination for an optimize lossless compression could be more expensive).

On Fri, 22 Apr 2022, 06:05 Mike Taves, <mwtoews at gmail.com> wrote:

> On Fri, 22 Apr 2022 at 07:05, <Matt.Wilkie at yukon.ca> wrote:
> >
> > Idea for a small but useful python tool: scan image for min/max values
> and convert to smallest possible bit depth without losing values. Surely
> someone has done something like this already. Any suggestions for where to
> look for prior art?
>
> This is driver-specific, as certain formats expect multiples of 2
> (e.g.) NBITS=1/2/4. But for GTiff, what I typically use in a script is
> to find the maximum value, then use "ceil(log(maxval, 2))" to get the
> number of bits, e.g.:
>
> from math import ceil, log
> from osgeo import gdal
>
> maxval = 17  # for example
> nbits = ceil(log(maxval, 2))  # 5
>
> drv = gdal.GetDriverByName("GTiff")
> opts = [f"NBITS={maxval}"]
> ds = drv.Create(fname, nx, ny, 1, gdal.GDT_Byte, opts)
> ...
>
> similar can be done with rasterio, passing the keyword
> "rasterio.open(fname, 'w', ..., nbits=nbits)"
>
> For the drivers that expect NBITS as a multiple of 2:
>
> nbits = 2**ceil(log(nbits, 2))
>
> If nbits is greater than 8, then UInt16 or UInt32 may be required, as
> supported by the driver.
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-d
> <https://lists.osgeo.org/mailman/listinfo/gdal-dev>e vroom
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20220422/14703f9e/attachment.html>


More information about the gdal-dev mailing list