【发布时间】:2020-07-21 09:43:22
【问题描述】:
您好,我正在尝试在 Mac OS 中使用 Python 运行多个操作系统命令
我想在终端中使用转换命令将图像转换为 tif 文件。不过我有 100 张图片要转换,我编写了 Python 程序以使其更容易。
import os
import subprocess
files = os.listdir("/Users/woonie/Downloads/test")
i=1
for file in files:
args=['convert',file,'-resize','100%','-type',"Grayscale","/Users/woonie/Downloads/test/kor.",i,"test.exp0.tif"]
subprocess.Popen(args)
i = i+1
convert filename -...- output_filename.exp0.tif 是转换命令的形式,所以我每次都需要更改文件名和 output_filename。我在文件中有文件名列表。我想在“测试”之后放置图像数量,使其变为 kor.test1.exp0.tif、kor.test2.exp0.tif 等。
Traceback (most recent call last):
File "/Users/woonie/PycharmProjects/image_chage/change.py", line 9, in <module>
subprocess.Popen(args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1637, in _execute_child
self.pid = _posixsubprocess.fork_exec(
TypeError: expected str, bytes or os.PathLike object, not int
但是出现了这个错误。
所以我把代码改成了
args=['convert'+str(file)+'-resize','100%','-type',"Grayscale","/Users/woonie/Downloads/test/kor."+str(i)+"test.exp0.tif"]
但同样的错误出现了...... 当我将该命令直接放入终端时,它就起作用了。
我是不是用错了子进程?或者我无法制作我想要的程序
【问题讨论】:
-
你确定这是你的真实代码吗?
args=['convert'+str(file)+'-resize'将产生convertFILENAME-resize作为第一个参数,这是行不通的。 -
是的,这是我的代码。那我应该把它写下来喊','吗?
标签: python command-line subprocess imagemagick imagemagick-convert