Porting 'sh 1.11'-based code to Windows

imagemagick-convert, porting, python, subprocess

Solution

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

Problem

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?

Original source

Related problems