使用 Python 写入图像文件时遇到问题

Having Trouble Writing Image Files with Python

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

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

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

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