GDAL对图像文件格式的转换

您所在的位置:网站首页 tif转换jpg GDAL对图像文件格式的转换

GDAL对图像文件格式的转换

2023-08-21 14:48| 来源: 网络整理| 查看: 265

我们常常在图像处理过程中遇到不同软件或程序要求输入的图像格式不同(有些程序或软件支持的数据格式不是常用的Tiff,Img等数据格式),因此需要对不同的数据格式相互进行转换。 我这里以GTiff(.tif)数据转换为PCRaster(.map)数据为例。

        首先需要安装GDAL,我这里是在Anaconda上直接安装了基于Python的GDAL,可以在下面网站自行下载,https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal   例如下面对应的就是Python3.8版本的GDAL。

        安装好后可在Anaconda的Prompt中直接使用gdal_translate.exe直接实现转换,转换方法如下:

gdal_translate [--help-general] [-ot {Byte/Int16/UInt16/UInt32/Int32/Float32/Float64/ CInt16/CInt32/CFloat32/CFloat64}] [-strict] [-if format]* [-of format] [-b band]* [-mask band] [-expand {gray|rgb|rgba}] [-outsize xsize[%]|0 ysize[%]|0] [-tr xres yres] [-r {nearest,bilinear,cubic,cubicspline,lanczos,average,rms,mode}] [-unscale] [-scale[_bn] [src_min src_max [dst_min dst_max]]]* [-exponent[_bn] exp_val]* [-srcwin xoff yoff xsize ysize] [-epo] [-eco] [-projwin ulx uly lrx lry] [-projwin_srs srs_def] [-a_srs srs_def] [-a_ullr ulx uly lrx lry] [-a_nodata value] [-a_scale value] [-a_offset value] [-nogcp] [-gcp pixel line easting northing [elevation]]* |-colorinterp{_bn} {red|green|blue|alpha|gray|undefined}] |-colorinterp {red|green|blue|alpha|gray|undefined},...] [-mo "META-TAG=VALUE"]* [-q] [-sds] [-co "NAME=VALUE"]* [-stats] [-norat] [-noxmp] [-oo NAME=VALUE]* src_dataset dst_dataset

根据需求,输入相关的参数进行转换,不需要的参数可以忽略。主要参数说明(其他参数详细见GDAL官方文档 gdal_translate — GDAL 文档):

        -ot  强制输出图像带具有驱动程序支持的特定数据类型,该数据类型可以是以下类型之一: Byte , UInt16 , Int16 , UInt32 , Int32 , Float32 , Float64 , CInt16 , CInt32 , CFloat32 或 CFloat64 .

        -if  试图打开输入文件的格式/驱动程序名称。通常不需要指定它,但当它无法选择适当的驱动程序时,可以使用它跳过自动驱动程序检测。

        -of  选择输出格式。从GDAL 2.3开始,如果未指定,则从扩展名猜测格式。

        -b 选择输入波段 band 输出,从1开始编号。

        -mask   选择输入波段 band 创建输出数据集掩码带区。

   -tr 设定目标分辨率。这些值必须用地理参考单位表示。两者都必须是正值。

        -r 采样方式,有nearest (default),bilinear,cubic,cubicspline,lanczos,average,rms,mode

  源数据集名称。它可以是文件名、数据源的URL或多数据集文件的子数据集名称。

  目标文件名。

支持的数据格式有:   VRT: Virtual Raster   GTiff: GeoTIFF   NITF: National Imagery Transmission Format   HFA: Erdas Imagine Images (.img)   ELAS: ELAS   AAIGrid: Arc/Info ASCII Grid   DTED: DTED Elevation Raster   PNG: Portable Network Graphics   JPEG: JPEG JFIF   MEM: In Memory Raster   GIF: Graphics Interchange Format (.gif)   XPM: X11 PixMap Format   BMP: MS Windows Device Independent Bitmap   PCIDSK: PCIDSK Database File   PCRaster: PCRaster Raster File   ILWIS: ILWIS Raster Map   SRTMHGT: SRTMHGT File Format   Leveller: Leveller heightfield   Terragen: Terragen heightfield   PNM: Portable Pixmap Format (netpbm)   ENVI: ENVI .hdr Labelled   EHdr: ESRI .hdr Labelled   PAux: PCI .aux Labelled   MFF: Vexcel MFF Raster   MFF2: Vexcel MFF2 (HKV) Raster   BT: VTP .bt (Binary Terrain) 1.3 Format   IDA: Image Data and Analysis   ERS: ERMapper .ers Labelled   FIT: FIT Image   RMF: Raster Matrix Format   RST: Idrisi Raster A.1   INGR: Intergraph Raster   GSAG: Golden Software ASCII Grid (.grd)   GSBG: Golden Software Binary Grid (.grd)   USGSDEM: USGS Optional ASCII DEM (and CDED)   ADRG: ARC Digitized Raster Graphics

使用实例:

         我这里将GTiff(.tif)数据转换为PCRaster(.map)数据,其他信息不变,但是出现了报错,大致内容是数据驱动上的问题。

        我再在参数中加入了数据 -ot(强制输出图像带具有驱动程序支持的特定数据类型)参数,转换成功。gdal_translate -ot Float32 -of PCRaster  D:\data\DEM.tif D:\data\DEM_test.map

        当然,gdal_translate工具还可以实现更改投影系统、波段选择、图像裁剪、调整图像分辨率等功能,只需要用好相关的参数即可。

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3