1

うわー、ここで私の問題を簡潔な見出しにカプセル化するのは難しかった. 私が管理したことを願っています。

convertImageMagick を使用してAmazon S3 から URL を取得しようとすると問題が発生する単純なサムネイル機能があります。通常、PIL を使用して画像ファイルを読み込んで変換しますが、PIL は PDF 形式を読み込めないため、サブプロセス呼び出しによる変換に頼っています。

これはdjangoのviews.pyのコードです。ここでの考え方は、S3 からファイル URL を取得し、それを で開きconvert、PNG に処理して stdout に送信し、出力されたバッファを使用して StringIO オブジェクトをロードし、それを default_storages に戻して保存するというものです。サムネイルファイルを S3 に戻します。このような単純な仕事にはかなりの失敗がありますが、これで終わりです。

注: convertHeroku を使用した本番環境でファイルを確実にディスクに保存することはできません。

def _large_thumbnail_s3(p):

    # get the URL from S3, trimming off the expiry info etc. So far so good.

    url = default_storage.url(p+'.pdf').split('?')
    url = url[0]

    # this opens the PDF file fine, and proceeds to convert and send 
    # the new PNG to the buffer via standard output.

    from subprocess import call 
    call("convert -thumbnail '400x600>' -density 96 -quality 85 "
        +url
        +" png:-", shell=True)

    from StringIO import StringIO

    # here's where the problem starts. I'm clearly not reading
    # in the stdout correctly, as I get a IOError: File not open for reading
    # from this next line of code:

    completeStdin = sys.stdout.read()
    im = StringIO(completeStdin)

    # now take the StringIO PNG object and save it back to S3 (this 
    # should not be an issue.

    im = default_storage.open(p+'_lg.png', 'w+b')
    im.close()

誰かが私に教えてもらえますか?a)出力をサムネイル関数に送り返すことに関してどこが間違っているのか、そしてb)これを行うかなりハッキーな方法に代わるより堅牢な代替案を提案できるかどうか!

ティア

4

1 に答える 1

1

subprocess.check_outputではなく、を使用する必要がありますsubprocess.call

from subprocess import check_output
from StringIO import StringIO

out, err = check_output("convert -thumbnail '400x600>' -density 96 -quality 85 "
    +url
    +" png:-", shell=True)

buffer = StringIO(out)
于 2013-07-22T15:45:05.863 に答える