gpt4 book ai didi

python - 将基于 'sh 1.11' 的代码移植到 Windows

转载 作者:太空宇宙 更新时间:2023-11-03 12:24:04 24 4
gpt4 key购买 nike

所有迹象似乎都表明我的脚本可以在 Linux 环境中完全运行,据我所知,唯一阻止它在 Windows 中运行的是我对 sh 的使用。 ,这非常简单:

from sh import convert

convert(inputfile, '-resize', r, '-quality', q, '-strip', outputfile)

这转换为 bash 行:

convert image.jpg -resize 350x350 -quality 80 -strip ./small/export.jpg

其中 rq 变量是任何给定的分辨率或质量。


在 Windows 中运行它当然会引发错误,因为“sh”在 Windows 中完全不起作用 :( 我尝试用已弃用的 pbs 替换“sh”,但我没有任何运气。这就是我的到目前为止:

import pbs

pbs.convert('-resize', r, '-quality', q, '-strip', inputfile, outputfile)

引发的错误是:

  File "C:\Python27\lib\site-packages\pbs.py", line 265, in _create
if not path: raise CommandNotFound(program)
pbs.CommandNotFound: convert

问题:

How do I successfully pass these ImageMagick commands from my script while in a Windows environment?

最佳答案

正在关注 Kevin Brotckeanswer ,这是我们使用的 hack:

try:
import sh
except ImportError:
# fallback: emulate the sh API with pbs
import pbs
class Sh(object):
def __getattr__(self, attr):
return pbs.Command(attr)
sh = Sh()

关于python - 将基于 'sh 1.11' 的代码移植到 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28618906/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com