Porting 'sh 1.11'-based code to Windows

久未见 提交于 2019-12-23 17:16:18

问题


All signs seem to indicate that my script is fully operational within a Linux environment and as far as I can tell, the only thing keeping it from working in Windows is my usage of sh, which is pretty straightforward:

from sh import convert

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

This translates to a bash line:

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

where the r and q variables are any given resolution or quality.


Running this in Windows of course raises an error because 'sh' is completely non-functional in Windows :( I tried replacing 'sh' with the deprecated pbs, but am not having any luck. This is what I've got so far:

import pbs

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

The error being raised is:

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

Question:

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


回答1:


Following Kevin Brotcke's answer, this is the hack we went with:

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()



回答2:


pbs.CommandNotFound error message is because pbs does not have a convert method. Instead you need to use the Command method:

import pbs
convert = pbs.Command('convert')

Now you can use it similar to sh:

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



回答3:


Sub process is your best bet. While, as you said it isn't the easiest to learn, it is really useful. I would look at this indepth tutorial. Of course, read the docs too.

As to your specific problem, sh has been around longer than pbs, so it almost certainly has more functions. Looking through the source (pbs.py), I found no function named convert(). Also, you changed the arguments you called from sh to pbs (you didn't put an inputfile). Finally, there is no function named convert() in sh.py from the git repo, so I suspect you are confusing it with convert from something else.

Beyond that, you should be able to use pbs and subprocess in conjunction.




回答4:


You could use stdlib's subprocess module, to run the command on Windows:

#!/usr/bin/env python
from subprocess import check_call

check_call(r'c:\path\to\ImageMagick\convert.exe image.jpg -resize 350x350 -quality 80 -strip small\export.jpg')

It is important to provide the full path with the file extension otherwise a different convert command may be chosen.

check_call() raises an exception if convert exits with non-zero status. You could use subprocess.call() and check the status manually instead.



来源:https://stackoverflow.com/questions/28618906/porting-sh-1-11-based-code-to-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!