# The MIT License # # Copyright (c) 2008 Jared Kuolt # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import Image, ImageEnhance, os, md5, datetime from django.http import HttpResponse, HttpResponseRedirect ens = { 'color':ImageEnhance.Color, 'bright':ImageEnhance.Brightness, 'contrast':ImageEnhance.Contrast, 'sharp':ImageEnhance.Sharpness } def _get_enhancer_value(f): f = abs(float(f)) return (f, 100)[f > 100] / 50.0 def show_image(request): url = request.GET['u'] ext = url.split('.')[-1] source_file = '/tmp/%s.%s' % (md5.new(url).hexdigest(), ext) dir = '/home/jared/superjared.com/public' date_dir = datetime.date.today().strftime('%Y/%m/%d') request_hash = md5.new(request.get_full_path()).hexdigest() end_uri = fn = '/imgr/%s/%s.%s' % (date_dir, request_hash, ext) fn = ''.join([dir, fn]) root_path = os.path.dirname(fn) if not os.path.exists(root_path): os.makedirs(root_path) elif os.path.exists(fn): return HttpResponseRedirect(end_uri) if not os.path.exists(source_file): os.system('wget -O %s %s' %(source_file, url)) image = Image.open(source_file) if image.mode != 'RGB': image = image.convert('RGB') for k, v in ens.iteritems(): if request.GET.has_key(k): value = _get_enhancer_value(request.GET[k]) if value != 1: enhancer = v(image) image = enhancer.enhance(value) if request.GET.has_key('rotate'): angle = int(request.GET['rotate']) % 360 image = image.rotate(angle, expand=True) if request.GET.has_key('thumb'): newsize = [int(x) for x in request.GET['thumb'].split('x')[:2]] if len(newsize) == 2: image.thumbnail(newsize, Image.ANTIALIAS) if request.GET.has_key('resize'): multiple = int(request.GET['resize']) / 100.0 if multiple > 3: multiple = 3 newsize = [x * multiple for x in image.size] image = image.resize(newsize, Image.ANTIALIAS) if request.GET.has_key('crop'): crop = [int(x) for x in request.GET['crop'].split('x')[:4]] image = image.crop(crop) image.save(fn) return HttpResponseRedirect(end_uri)