title: Converting a 16bit GeoTiff file as an 8bit JPEG file using python
body: <p>I am trying to convert a 16 bit 3-band RGB GeoTIFF file into an 8 bit 3-band JPEG file. It seems like the <code>gdal</code> library should work well for this. <strong>My question is how do I specify the conversion to 8-bit output in the python gdal API, and how do I scale the values in that conversion? Also, how do I check to tell whether the output is 8-bit or 16-bit?</strong></p>
  
  <p>The <code>gdal.Translate()</code> function should serve the purpose. However, the only example that I found which will rescale the values to 8 bit involves the C interface. The two posts below provide examples of this, but again they do not suit my purpose because they are not using the Python interface. </p>
  
  <p><a href="https://gis.stackexchange.com/questions/26249/how-to-convert-qgis-generated-tiff-images-into-jpg-jpeg-using-gdal-command-line/26252">https://gis.stackexchange.com/questions/26249/how-to-convert-qgis-generated-tiff-images-into-jpg-jpeg-using-gdal-command-line/26252</a></p>
  
  <p><a href="https://gis.stackexchange.com/questions/206537/geotiff-to-16-bit-tiff-png-or-bmp-image-for-heightmap/206786">https://gis.stackexchange.com/questions/206537/geotiff-to-16-bit-tiff-png-or-bmp-image-for-heightmap/206786</a></p>
  
  <p>The python code that I came up with is:</p>
  
  <pre><code>from osgeo import gdal
  gdal.Translate(destName='test.jpg', srcDS='test.tif')
  </code></pre>
  
  <p>This will work, but I don't think the output is coverted to 8-bit or that the values are rescaled. Does anyone know how to apply those specific settings?</p>
  
  <p>Note that this post below is very similar, but uses the <code>PIL</code> package. However the problem is that apparently <code>PIL</code> has trouble ingesting 16 bit images. When I tried this code I got errors about reading the data. Hence, I could not use this solution. </p>
  
  <p><a href="https://stackoverflow.com/questions/28870504/converting-tiff-to-jpeg-in-python">converting tiff to jpeg in python</a></p>