Porting 'sh 1.11'-based Code To Windows
Solution 1:
Following Kevin Brotcke's answer, this is the hack we went with:
try:
import sh
except ImportError:
# fallback: emulate the sh API with pbsimport pbs
classSh(object):
def__getattr__(self, attr):
return pbs.Command(attr)
sh = Sh()
Solution 2:
pbs.CommandNotFound error message is because pbs does not have a convert method. Instead you need to use the Command method:
importpbsconvert= pbs.Command('convert')
Now you can use it similar to sh:
convert(inputfile, '-resize', r, '-quality', q, '-strip', outputfile)
Solution 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.
Solution 4:
You could use stdlib's subprocess module, to run the command on Windows:
#!/usr/bin/env pythonfrom 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.
Post a Comment for "Porting 'sh 1.11'-based Code To Windows"