麻烦用Python编写图像文件

时间:2016-10-26 13:48:43

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

将图像文件写入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'])

1 个答案:

答案 0 :(得分:0)

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

#!/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'])