Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert geotiff to jpg in python or java?

i have a geotiff images that have 3bands.

band1,2 is a actual image values and band3 is a instance angle value.

band1,2 is float32 data type

under code is that i try before.

but it doesn't work.

i think band data's range is too large, so it doesn't

from osgeo import gdal, osr, ogr
from PIL import Image
import numpy as np


ds = gdal.Open('image path', gdal.GA_ReadOnly)
rb = ds.GetRasterBand(1)
test = rb.ReadAsArray()
rb2 = ds.GetRasterBand(2)
test2 = rb2.ReadAsArray()
rb3 = ds.GetRasterBand(3)
test3 = rb3.ReadAsArray()
slice56 = test2
formatted = (slice56 * 255 / np.max(slice56)).astype('uint8')
img = Image.fromarray(formatted)
img.save('save image path')

how can i solve this problem??

like image 716
송준석 Avatar asked Dec 14 '22 16:12

송준석


1 Answers

You can use gdal.Translate for this.

You can read the documentation here

from osgeo import gdal
    
options_list = [
    '-ot Byte',
    '-of JPEG',
    '-b 1',
    '-scale'
]           

options_string = " ".join(options_list)
    
gdal.Translate(
    'save_image_path.jpg',
    'image_path.tif',
    options=options_string
)

The above code simply create a jpg file with band 1 scaled into byte range. You could add more bands by adding, '-b 2' etc. Also notice that scale automatically wraps the entire range into byte range. If you like something else you could use '-scale min_val max_val' in order to specify the range you like, since often you have no need of either the lowest or highest values available.

like image 84
the_cheff Avatar answered Dec 24 '22 03:12

the_cheff