I was able to convert my GeoTIFF images (from the SpaceNet dataset) to JPEGs using gdal_translate but I would like to know how to convert the JPEGs back to GeoTIFF. I've noticed that gdal_translate also creates an auxillary file with extension jpg.aux.xml when I do the conversion. Is this to be used for the conversion from JPEG to GeoTIFF? If so, how can this be done?
GDAL Translate JPEG to GeoTIFF Using Python – Step-by-Step Guide
.jpggdalgdal-translategeotiff-tiff
Related Solutions
Convert 0 to NoData:
gdal_translate -a_nodata 0 -of VRT RGB-PanSharpen_AOI_2_Vegas_img1.tif RGB-PanSharpen_AOI_2_Vegas_img1.vrt
Find image stats:
gdalinfo RGB-PanSharpen_AOI_2_Vegas_img1.vrt -stats
Driver: VRT/Virtual Raster
Files: RGB-PanSharpen_AOI_2_Vegas_img1.vrt
/tmp/RGB-PanSharpen_AOI_2_Vegas_img1.tif
Size is 650, 650
<snip>
Band 1 Block=128x128 Type=UInt16, ColorInterp=Gray
Min=30.000 Max=1525.000
Minimum=30.000, Maximum=1525.000, Mean=465.622, StdDev=221.362
NoData Value=0
Metadata:
STATISTICS_MAXIMUM=1525
STATISTICS_MEAN=465.62191430449
STATISTICS_MINIMUM=30
STATISTICS_STDDEV=221.36191405998
Band 2 Block=128x128 Type=UInt16, ColorInterp=Undefined
Min=150.000 Max=1772.000
Minimum=150.000, Maximum=1772.000, Mean=597.099, StdDev=208.563
NoData Value=0
Metadata:
STATISTICS_MAXIMUM=1772
STATISTICS_MEAN=597.09910596959
STATISTICS_MINIMUM=150
STATISTICS_STDDEV=208.56265835534
Band 3 Block=128x128 Type=UInt16, ColorInterp=Undefined
Min=171.000 Max=1202.000
Minimum=171.000, Maximum=1202.000, Mean=450.385, StdDev=118.278
NoData Value=0
Metadata:
STATISTICS_MAXIMUM=1202
STATISTICS_MEAN=450.38523089919
STATISTICS_MINIMUM=171
STATISTICS_STDDEV=118.27755953022
Use band min/max values to scale (or you could calculate a stddev scale or perhaps gdalinfo -hist etc...
to calculate a percentage stretch)
gdal_translate -scale_1 30 1525 -scale_2 150 1772 -scale_3 171 1202 -ot Byte -of JPEG RGB-PanSharpen_AOI_2_Vegas_img1.vrt RGB-PanSharpen_AOI_2_Vegas_img1.jpg
Or to output a white background:
gdalbuildvrt -srcnodata 0 -vrtnodata 65535 RGB-PanSharpen_AOI_2_Vegas_img1.vrt RGB-PanSharpen_AOI_2_Vegas_img1.tif
gdalinfo -stats etc...
gdal_translate etc...
Found a solution to my problem:
gdaldem color-relief -nearest_color_entry -alpha -co format=png -of KMLSUPEROVERLAY raster.tif color.txt raster-kml.kmz
gdaldem color-relief converts a grayscale tiff to a multiband RGB(A) tiff
'-alpha' says my color table has an alpha band (which controls transparency)
'-co format=png' outputs the tiles in .png instead of the default .jpg, because the latter does not support transparency (How can I create a kml file from a geotiff with alpha layer)
'-of KMLSUPEROVERLAY' says my output type is kml/kmz. I specified a .kmz extension because it puts all the tile layers/kmls into one file that GoogleEarth can read
Lastly, color.txt looks like this:
0 240 240 240 0
1 205 205 205 255
20 214 174 250 255
30 203 161 240 255
40 192 149 231 255
50 181 136 222 255
60 171 124 213 255
70 160 111 203 255
80 149 99 194 255
90 138 86 185 255
101 128 74 176 255
nv 240 240 240 0
Values of 0 and NoData are fully transparent, 1-20 is gray, and 20-100 have a light-dark purple gradient with classes of 10.
Best Answer
As the manual says, you can translate a raster image to GeoTIFF by specifying the appropriate driver:
Note: In this particular case, ensure that the jpg.aux.xml file that was created when converting from geotiff to jpg is present in the same directory as the image you want to convert. This auxiliary file contains the all the necessary geospatial info and is used by gdal when converting back to geotiff.