Login

ImageMagick commands from Django Templates

Author:
cogat
Posted:
April 26, 2009
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

A template filter which wraps imagemagick's convert command. The filter acts upon a source image path, and returns the filtered image path.

usage: {{ source_path|convert:"-resize 64x64\!" }}

The filter parameter is the processing arguments for an ImageMagick 'convert' command. See e.g. http://www.imagemagick.org/Usage/resize/

Every image created is saved in a cache folder. This code does not handle removing obsolete cached images. If the filtered image path exists already, no image processing is carried out, and the path is returned.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""
A template filter which wraps imagemagick's `convert` command. The filter acts upon a
source image path, and returns the filtered image path.
    
    usage: {{ source_path|convert:"-resize 64x64\!" }}
    
Example setting:
IMAGE_CACHE_PATH = "image_cache/"

I have tried to eliminate command injection from a template by escaping the parameters,
but am not an expert in such things - if anyone spots any holes, please leave a comment
or email to greg AT interactionconsortium DOT com.
    
"""

import os
from django import template
from django.conf import settings
import commands

register = template.Library()

"""
A convert filter.

Takes the file name (relative to MEDIA_ROOT), and a specification of the conversion
Returns a file name (relative to MEDIA_ROOT) of the converted file.

Pseudocode for filter:

1. generate the result filename.
2. does it exist? Yes = return it. No = create it.
3. do the conversion; save the file as the result filename.


"""
@register.filter
def convert(original_image_path, arg):
    if not original_image_path:
        return ''
        
    if arg == "":
        return original_image_path
    
    
    basename, format = original_image_path.rsplit(".", 1)
    basename, name = basename.rsplit(os.path.sep, 1)
    
    dest_folder = os.path.join(settings.IMAGE_CACHE_PATH, basename) 
    abs_dest_folder = os.path.join(settings.MEDIA_ROOT, dest_folder)
    
    arghash = str(hash(arg))
    
    dest_path = os.path.join(dest_folder, name+"_"+arghash+"."+format)
    abs_dest_path = os.path.join(settings.MEDIA_ROOT, dest_path)

    if os.path.exists(abs_dest_path):
        return dest_path
    
    if not os.path.exists(abs_dest_folder):
        os.makedirs(abs_dest_folder)
        
    abs_source_path = os.path.join(settings.MEDIA_ROOT, original_image_path)
    
    if not os.path.exists(abs_source_path):
        return "%s<-NOT FOUND" % original_image_path
    
    #escape strings so as to avoid injections
    c_source = str(abs_source_path).encode('string-escape')
    c_arg = str(arg).encode('string-escape')
    
    cmd = "convert %s %s %s" % (c_source, c_arg, abs_dest_path)
    
    status, result = commands.getstatusoutput(cmd)
    
    if status == 0:
        return dest_path
    else:
        return "%s<-IMAGEMAGICK RETURNED STATUS %s" % (original_image_path, status)
    
    
    

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.