java操作geotiff,如何在python或java中将geotiff转换为jpg?

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??

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值