0

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.

ensnare
  • 40,069
  • 64
  • 158
  • 224

1 Answers1

1

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
gbin
  • 2,960
  • 1
  • 14
  • 11
  • Will this use a lot of memory? – ensnare Aug 30 '12 at 20:40
  • If you pipe the process out to another descriptor, the memory usage should be minimal: only the convert process buffers which I completely guess depending on its implementation but should be once the size of the decompressed image + the size of the decompressed thumbnail. If they have done better than that they have a really good internal implementation ;) – gbin Aug 30 '12 at 20:48
  • Thanks. How could I extend this to generate multiple thumbnail sizes? If I put in another subprocess.Popen(...) line with different dimensions, would it re-download the entire full image from S3? – ensnare Aug 30 '12 at 21:55
  • Every other time I execute the above code, I get the following error: convert: improper image header `-' @ error/png.c/ReadPNGImage/3246. convert: missing an image filename `jpg:-' @ error/convert.c/ConvertImageCommand/3011. – ensnare Aug 31 '12 at 00:05