簡體   English   中英

使用 python 將 Tif 文件轉換為 RGB(png/jpg)

[英]To convert Tif files into RGB(png/jpg) using python

我正在使用下面給出的代碼快照,它的工作沒有錯誤,但轉換后的文件沒有.png 擴展名,因為我在“OutputFormat”中給出了 png。 我在 Colab 中運行它,並且還附加了 output。

from osgeo import gdal
import numpy as np
import os
import subprocess

def _16bit_to_8Bit(inputRaster, outputRaster, outputPixType='Byte', outputFormat='png', 
percentiles=[2, 98]):

#Convert 16bit image to 8bit
#Source: Medium.com, 'Creating Training Datasets for the SpaceNet Road Detection and Routing 
#Challenge' by Adam Van Etten and Jake Shermeyer

    srcRaster = gdal.Open(inputRaster)
    cmd = ['gdal_translate', '-ot', outputPixType, '-of', 
       outputFormat]

    # iterate through bands
    for bandId in range(srcRaster.RasterCount):
        bandId = bandId+1
        band = srcRaster.GetRasterBand(bandId)

        bmin = band.GetMinimum()        
        bmax = band.GetMaximum()
        # if not exist minimum and maximum values
        if bmin is None or bmax is None:
            [enter image description here][1](bmin, bmax) = band.ComputeRasterMinMax(1)
        # else, rescale
        band_arr_tmp = band.ReadAsArray()
        bmin = np.percentile(band_arr_tmp.flatten(), 
                             percentiles[0])
        bmax= np.percentile(band_arr_tmp.flatten(), 
                             percentiles[1])

        cmd.append('-scale_{}'.format(bandId))
        cmd.append('{}'.format(bmin))
        cmd.append('{}'.format(bmax))
        cmd.append('{}'.format(0))
        cmd.append('{}'.format(255))
    cmd.append(inputRaster)
    cmd.append(outputRaster)
    print("Conversin command:", cmd)
    subprocess.call(cmd)

path = "/content/drive/MyDrive/Spacenet_data/RGB_Pan/"
files = os.listdir(path)

for file in files:
   resimPath = path+file
   dstPath   = "/content/drive/MyDrive/Spacenet_data/"
   dstPath   = dstPath+file
   _16bit_to_8Bit(resimPath,dstPath)

我的 output 顯示如下:

Conversin command: ['gdal_translate', '-ot', 'Byte', '-of', 'png', '-scale_1', '149.0', '863.0', '0', '255', '-scale_2', '244.0', '823.0200000000186', '0', '255', '-scale_3', '243.0', '568.0', '0', '255', '/content/drive/MyDrive/Spacenet_data/RGB_Pan/img0.tif', '/content/drive/MyDrive/Spacenet_data/img0.tif']

進行以下更改,您就完成了。

from osgeo import gdal
import numpy as np
import os
import subprocess

def _16bit_to_8Bit(inputRaster, outputRaster, outputPixType='Byte', 
outputFormat='png', percentiles=[2, 98]):

   srcRaster = gdal.Open(inputRaster)
   cmd = ['gdal_translate', '-ot', outputPixType, '-of', 
          outputFormat]

   for bandId in range(srcRaster.RasterCount):
       bandId = bandId+1
       band = srcRaster.GetRasterBand(bandId)

       bmin = band.GetMinimum()        
       bmax = band.GetMaximum()
       # if not exist minimum and maximum values
       if bmin is None or bmax is None:
           (bmin, bmax) = band.ComputeRasterMinMax(1)
       # else, rescale
       band_arr_tmp = band.ReadAsArray()
       bmin = np.percentile(band_arr_tmp.flatten(), 
                           percentiles[0])
       bmax= np.percentile(band_arr_tmp.flatten(), 
                           percentiles[1])

       cmd.append('-scale_{}'.format(bandId))
       cmd.append('{}'.format(bmin))
       cmd.append('{}'.format(bmax))
       cmd.append('{}'.format(0))
       cmd.append('{}'.format(255))

   cmd.append(inputRaster)
   cmd.append(outputRaster)
   print("Conversin command:", cmd)
   subprocess.call(cmd)

path = "/content/drive/MyDrive/Spacenet_data/RGB_Pan/"
files = os.listdir(path)

for file in files:
    resimPath = path+file
    dstPath   = "/content/drive/MyDrive/Spacenet_data/"
    dstPath   = dstPath+file[:-3]+"png"

    _16bit_to_8Bit(resimPath,dstPath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM