django - Sending standard output from command line function to python StringIO object -
wow, hard encapsulate issue here succinct headline. hope managed.
i've got simple thumbnail feature causing me issues when try retrieve url amazon s3, convert
using imagemagick. use pil read in image file , convert it, pil doesn't read in pdf formats, i'm resorting convert through subprocess call.
here's code django views.py. idea here file url s3, open convert
, process png, send stdout, , use outputted buffer load stringio object, gets passed default_storages save thumbnail file s3. quite faff such simple job, there go.
please note: cannot reliably save file disk using convert
on production set-up heroku, otherwise, i'd doing already.
def _large_thumbnail_s3(p): # url s3, trimming off expiry info etc. far good. url = default_storage.url(p+'.pdf').split('?') url = url[0] # opens pdf file fine, , proceeds convert , send # new png buffer via standard output. subprocess import call call("convert -thumbnail '400x600>' -density 96 -quality 85 " +url +" png:-", shell=true) stringio import stringio # here's problem starts. i'm not reading # in stdout correctly, ioerror: file not open reading # next line of code: completestdin = sys.stdout.read() im = stringio(completestdin) # take stringio png object , save s3 (this # should not issue. im = default_storage.open(p+'_lg.png', 'w+b') im.close()
can tell me a) might going wrong regards sending output thumbnail function, , b) whether can suggest more robust alternatives seems pretty hacky way of doing this!
tia
you need use subprocess.check_output
, not subprocess.call
:
from subprocess import check_output stringio import stringio out, err = check_output("convert -thumbnail '400x600>' -density 96 -quality 85 " +url +" png:-", shell=true) buffer = stringio(out)
Comments
Post a Comment