如何在python或java中将geotiff转换为jpg? [英] how to convert geotiff to jpg in python or java?
问题描述
我有一个3波段的Geotiff图像.
i have a geotiff images that have 3bands.
band1,2是实际图像值,band3是实例角度值.
band1,2 is a actual image values and band3 is a instance angle value.
band1,2是float32数据类型
band1,2 is float32 data type
在代码下是我以前尝试过的.
under code is that i try before.
但是它不起作用.
我认为频段数据的范围太大,所以不会
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??
推荐答案
您可以为此使用gdal.Translate
.
您可以在此处
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
)
上面的代码仅创建了一个带1缩放到字节范围的jpg文件.您可以通过添加,'-b 2'
等添加更多的频段.还要注意,小数位会自动将整个范围包装为字节范围.如果您还喜欢其他东西,可以使用'-scale min_val max_val'
来指定所需的范围,因为通常您不需要可用的最低或最高值.
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.
这篇关于如何在python或java中将geotiff转换为jpg?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!