【问题标题】:To convert Tif files into RGB(png/jpg) using python使用 python 将 Tif 文件转换为 RGB(png/jpg)
【发布时间】:2021-02-24 19:22:36
【问题描述】:

我正在使用下面给出的代码快照,它的工作没有错误,但转换后的文件没有 .png 扩展名,因为我在“OutputFormat”中给出了 png。 我在 Colab 中运行它,我也在附加输出。

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)

我的输出显示如下:

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']

【问题讨论】:

    标签: rgb tiff geotiff format-conversion


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:
      import os
      import cv2
      
      directory = os.fsencode(r"path")
      
      for file in os.listdir(directory):
          filename = os.fsdecode(file)
          if filename.endswith(".tif"):
              print(filename)
              print(type(filename))
              print("\n")
              image = cv2.imread(filename)
              cv2.imwrite("{}.jpg".format(filename), image)
              continue
          else:
              continue
      

      【讨论】:

        猜你喜欢
        • 2023-03-05
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        • 1970-01-01
        • 2020-05-19
        • 1970-01-01
        相关资源
        最近更新 更多