Search code examples
pythonpopenstringio

MozJPEG STDIN Mode with switches


I'm able to get stdin/out working just fine with mozjpeg 3 if I just run without any flags. Example(Python):

fp = urllib.urlopen(http://path.to/unoptimized.jpg)
out_im2 = StringIO.StringIO(fp.read()) # StringIO Image
subp = subprocess.Popen(["/home/ubuntu/mozjpeg/cjpeg"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
    image_results = subp.communicate(input=out_im2.getvalue())

If, however, I attempt to customize this with switches(For example "-quality 70"), I cannot get it to work. I'm not sure if this is a bug or if I'm missing something. Any insight would be greatly appreciated:

subp = subprocess.Popen(["/home/ubuntu/mozjpeg/cjpeg", "-quality 70"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
image_results = subp.communicate(input=out_im2.getvalue())

Upon this execution, I receive the following back:

/home/ubuntu/mozjpeg/.libs/lt-cjpeg: unknown option 'quality 70'
usage: /home/ubuntu/mozjpeg/.libs/lt-cjpeg [switches] [inputfile]
Switches (names may be abbreviated):
  -quality N[,...]   Compression quality (0..100; 5-95 is useful range)
.... <Rest of --help screen>

Thanks in advance for any help.


Solution

  • Thanks to https://github.com/njdoyle “-quality 70” should be “-quality”, “70”. Two parameters.

    So:

    subp = subprocess.Popen(["/home/ubuntu/mozjpeg/cjpeg", "-quality","70"],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
    image_results = subp.communicate(input=out_im2.getvalue())