简体   繁体   English

在 EXE pyinstaller 中调用 subprocess.checkoutput()

[英]Calling subprocess.checkoutput() in EXE pyinstaller

I am trying to merge multiple tiff files into one multi-band tiff using python.我正在尝试使用 python 将多个 tiff 文件合并为一个多波段 tiff。 I used subprocess.checkoutput('python gdal.merge.py') and it was working well.我使用了 subprocess.checkoutput('python gdal.merge.py') 并且运行良好。 Then I tried to transform this code to an EXE using pyinstaller, it returns an error.然后我尝试使用pyinstaller将此代码转换为EXE,它返回错误。 the error is that python was not found.错误是未找到 python。 This is the code I used这是我使用的代码

basename = '*'
outdir2 = './TIFF_Files'
filelist2 = glob.glob(os.path.join(outdir2, basename))
files_string = " ".join(filelist2)
print(files_string)
Tiff_output = os.path.join(outdir2,'Multi_band.tif')
command = "python gdal_merge.py -o " + Tiff_output + " -of gtiff -separate " + files_string
output = subprocess.check_output(command)

the error of the EXE is the following: EXE的错误如下:

Python was not found; run without arguments to install from the Miscrosoft Store, or disable this shortcut from Settings

Can you help me with this.你能帮我解决这个问题吗? keep in mind that I am not really advanced in python so I apologize if I made an obvious mistake.请记住,我在 python 方面并不是很先进,所以如果我犯了一个明显的错误,我深表歉意。 Thanks谢谢

I found a way to replace the external process by using rasterio.我找到了一种使用 rasterio 替换外部进程的方法。

filelist2 = glob.glob(os.path.join(outdir2, basename))

# Read metadata of first file
with rasterio.open(filelist2[0]) as src0:
    meta = src0.meta

# Update meta to reflect the number of layers
meta.update(count = len(filelist2))

# Read each layer and write it to stack
Tiff_output = os.path.join(outdir2,'Multi_band.tif')
with rasterio.open(Tiff_output, 'w', **meta) as dst:
    for id, layer in enumerate(filelist2, start=1):
        with rasterio.open(layer) as src1:
            dst.write_band(id, src1.read(1))

outdir2 is the directory of the tiff files and basename = '*.tif' Using this method, Pyinstaller worked perfectly outdir2 是 tiff 文件的目录和 basename = '*.tif' 使用这种方法,Pyinstaller 工作得很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM