画像を取得して最適化し、クラウドストレージに保存するシンプルなアプリに取り組んでいます。ファイルを取得し、PIL を使用して最適化する例を見つけました。コードは次のようになります。
def inPlaceOptimizeImage(photo_blob):
blob_key = photo_blob.key()
new_blob_key = None
img = Image.open(photo_blob.open())
output = StringIO.StringIO()
img.save(output,img.format, optimized=True,quality=90)
opt_img = output.getvalue()
output.close()
# Create the file
file_name = files.blobstore.create(mime_type=photo_blob.content_type)
# Open the file and write to it
with files.open(file_name, 'a') as f:
f.write(opt_img)
# Finalize the file. Do this before attempting to read it.
files.finalize(file_name)
# Get the file's blob key
return files.blobstore.get_blob_key(file_name)
これはローカルでは問題なく動作します (ただし、アップロードされた画像をhttp://www.jpegmini.com/などで実行すると、まだ 2.4 倍に縮小されるため、最適化されているかどうかはわかりません)。ただし、アプリをデプロイして画像をアップロードしようとすると、頻繁に 500 エラーが発生し、ログに次のメッセージが記録されます。
F 00:30:33.322 Exceeded soft private memory limit of 128 MB with 156 MB after servicing 7 requests total
W 00:30:33.322 While handling this request, the process that handled this request was found to be using too much memory and was terminated. This is likely to cause a new process to be used for the next request to your application. If you see this message frequently, you may have a memory leak in your application.
2 つの質問があります。
- これは、画像を最適化してクラウド ストレージに保存するための最良の方法ですか?
- これらの 500 エラーが発生しないようにするにはどうすればよいですか?
前もって感謝します。