<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0cm;
mso-margin-bottom-alt:auto;
margin-left:0cm;
font-size:12.0pt;
font-family:"Times New Roman",serif;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:#1F497D;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-CA" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US">Thank you both for this, very helpful.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span lang="EN-US" style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D">-Matt</span><i><span style="font-size:10.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p></o:p></span></i></p>
</div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D;mso-fareast-language:EN-US"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span lang="EN-US" style="font-size:11.0pt;font-family:"Calibri",sans-serif"> Idan Miara <idan@miara.com>
<br>
<b>Sent:</b> April 21, 2022 11:18 PM<br>
<b>To:</b> Mike Taves <mwtoews@gmail.com><br>
<b>Cc:</b> Matt.Wilkie <Matt.Wilkie@yukon.ca>; gdal dev <gdal-dev@lists.osgeo.org><br>
<b>Subject:</b> Re: [gdal-dev] Convert to min containing bit depth?<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal">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). <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Fri, 22 Apr 2022, 06:05 Mike Taves, <<a href="mailto:mwtoews@gmail.com" target="_blank">mwtoews@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm">
<p class="MsoNormal">On Fri, 22 Apr 2022 at 07:05, <<a href="mailto:Matt.Wilkie@yukon.ca" target="_blank">Matt.Wilkie@yukon.ca</a>> wrote:<br>
><br>
> 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?<br>
<br>
This is driver-specific, as certain formats expect multiples of 2<br>
(e.g.) NBITS=1/2/4. But for GTiff, what I typically use in a script is<br>
to find the maximum value, then use "ceil(log(maxval, 2))" to get the<br>
number of bits, e.g.:<br>
<br>
from math import ceil, log<br>
from osgeo import gdal<br>
<br>
maxval = 17 # for example<br>
nbits = ceil(log(maxval, 2)) # 5<br>
<br>
drv = gdal.GetDriverByName("GTiff")<br>
opts = [f"NBITS={maxval}"]<br>
ds = drv.Create(fname, nx, ny, 1, gdal.GDT_Byte, opts)<br>
...<br>
<br>
similar can be done with rasterio, passing the keyword<br>
"rasterio.open(fname, 'w', ..., nbits=nbits)"<br>
<br>
For the drivers that expect NBITS as a multiple of 2:<br>
<br>
nbits = 2**ceil(log(nbits, 2))<br>
<br>
If nbits is greater than 8, then UInt16 or UInt32 may be required, as<br>
supported by the driver.<br>
_______________________________________________<br>
gdal-dev mailing list<br>
<a href="mailto:gdal-dev@lists.osgeo.org" target="_blank">gdal-dev@lists.osgeo.org</a><br>
<a href="https://can01.safelinks.protection.outlook.com/?url=https%3A%2F%2Flists.osgeo.org%2Fmailman%2Flistinfo%2Fgdal-dev&data=05%7C01%7CMatt.Wilkie%40yukon.ca%7C530d4395252944fbff3a08da2427e83d%7C98f515313973490abb70195aa264a2bc%7C0%7C0%7C637862051196803406%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Wxzbgz3TEIvRtrCiUvFSGc5CUoGIbeeCC8VXt1wtfuA%3D&reserved=0" target="_blank">https://lists.osgeo.org/mailman/listinfo/gdal-d</a>e
vroom<o:p></o:p></p>
</blockquote>
</div>
</div>
</div>
</div>
</div>
</body>
</html>