Skip to content

Instantly share code, notes, and snippets.

@balazs-endresz
Created June 2, 2014 13:35
Show Gist options
  • Save balazs-endresz/2fec55cccf7dd541328c to your computer and use it in GitHub Desktop.
Save balazs-endresz/2fec55cccf7dd541328c to your computer and use it in GitHub Desktop.
Convert supported files to an RGB image with Wand/ImageMagick
import subprocess
from django.core.mail import mail_admins
def convert_to_rgb(local_file):
"""
Returns an RGB Wand image or one with the original colourspace if the conversion fails.
Colourspace conversion is currently not working with the Wand API: https://github.com/dahlia/wand/issues/110
"""
in_filename = '%s[0]' % local_file.name
out_filename = '%s_rgb.jpg' % local_file.name
p = subprocess.Popen(['convert',
'-quality', '90',
'-density', '200',
'-colorspace', 'RGB',
in_filename, out_filename
], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if err:
mail_admins("Image conversion error", "%s\n%s" % (local_file.name, err))
local_file.seek(0)
return WandImage(file=local_file)
else:
return WandImage(file=open(out_filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment