Using ImageMagick to generate thumbnails for images stored in Amazon S3
-
29-06-2021 - |
문제
I'm looking to use ImageMagick's convert utility to make thumbnails for images stored in S3. I'm writing this in Python.
How should I approach this?
Should I read the image from S3, save it to a temporary folder on an EC2 instance, generate the thumbnail to a temporary directory, then put the temporary file into S3 and delete it?
Or can I "pipe" the result from S3 right into ImageMagick without storing it to local disk?
Is there a recommended way to do this?
Thanks in advance.
해결책
You can use subprocess with file objects and directly pipe them to imagemagick. For example here I convert directly an online png to a jpg without using temporary files.
import subprocess
import urllib2
import sys
source = urllib2.urlopen('http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png')
p = subprocess.Popen(['convert','png:-', 'jpg:-'], stdin=source, stdout=subprocess.PIPE)
p.communicate()[0] # this is your converted image
제휴하지 않습니다 StackOverflow