【问题标题】:Having Trouble Writing Image Files with Python使用 Python 编写图像文件时遇到问题
【发布时间】:2016-10-26 13:48:43
【问题描述】:

我在将图像文件写入 PC 上的文件夹时遇到了一些问题。 正如您在下面的脚本中看到的那样,我使用了 os.path.expanduser,因为我打算使用将此脚本发送到另一台计算机,该计算机显然与我的本地 PC 没有相同的文件路径。当我运行这个脚本时,gdal_calc 运行正常,但是,没有输出?任何关于如何创建输出文件或可能导致图像消失的想法将不胜感激。

#!/usr/bin/env bash

import subprocess
from subprocess import call
import sys
import os


# Set constants
# The pathway to the image files are nested within the '--outfile=' command

inHVFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosHV-Test-3.img')
outPlacement = os.path.expanduser('~\\Desktop\\newHVZeros.img')
outVFile = '--outfile=outPlacement'
outVFile_1 = '--outfile=outPlacement_1'
inVHFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosVH-Test-3.img')
outPlacement_1 = os.path.expanduser('~\\Desktop\\newVHZeros.img')



subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inHVFile, outVFile, '--calc=A+1'])

subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inVHFile, outVFile_1, '--calc=A+1'])

【问题讨论】:

    标签: python-3.x operating-system gdal


    【解决方案1】:

    您的脚本可能存在一些问题。我在下面对它们进行了注释。

    #!/usr/bin/env bash
    #              ^^^^  THIS ISN'T A BASH SCRIPT, THE ENVIRONMENT IS PYTHON
    
    import subprocess
    from subprocess import call
    import sys
    import os
    
    
    # Set constants
    # The pathway to the image files are nested within the '--outfile=' command
    # YOU CANNOT NEST A VARIABLE INSIDE A STRING LIKE YOU ARE TRYING TO DO
    
    inHVFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosHV-Test-3.img')
    outPlacement = os.path.expanduser('~\\Desktop\\newHVZeros.img')
    outVFile = '--outfile=outPlacement'
    #                     ^^^^^^^^^^^^ THIS WILL NOT EXPAND TO YOUR outPlacement VARIABLE
    outVFile_1 = '--outfile=outPlacement_1'
    #                       ^^^^^^^^^^^^^^ THIS WILL NOT EXPAND TO YOUR outPlacement_1 VARIABLE
    inVHFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosVH-Test-3.img')
    outPlacement_1 = os.path.expanduser('~\\Desktop\\newVHZeros.img')
    
    
    
    subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inHVFile, outVFile, '--calc=A+1']) 
    # This call to gdal_calc.py will produce a file called "outPlacement.tif" in whatever directory the script is run
    
    subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inVHFile, outVFile_1, '--calc=A+1'])
    # This call to gdal_calc.py will produce a file called "outPlacement_1.tif" in whatever directory the script is run
    

    这是一个正确的版本:

    #!/usr/bin/env python
    
    import subprocess
    import sys
    import os
    
    # Set constants
    inHVFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosHV-Test-3.img')
    outPlacement = os.path.expanduser('~\\Desktop\\newHVZeros.img')
    outVFile = '--outfile=' + outPlacement
    
    inVHFile = os.path.expanduser('~\\Desktop\\Zeros\\ZerosVH-Test-3.img')
    outPlacement_1 = os.path.expanduser('~\\Desktop\\newVHZeros.img')
    outVFile_1 = '--outfile=' + outPlacement_1
    
    subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inHVFile, outVFile, '--calc=A+1'])
    subprocess.call([sys.executable, 'C:\\Program Files (x86)\\GDAL\\gdal_calc.py','-A', inVHFile, outVFile_1, '--calc=A+1'])
    

    【讨论】:

      猜你喜欢
      • 2021-11-27
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2017-04-30
      • 2021-06-19
      • 1970-01-01
      相关资源
      最近更新 更多