def crop(request, morph_id): try: morph = Morph.objects.get(id=morph_id) if morph.final: return redirect(label='morphin:view', args=[morph_id]) except: not_found() if request.method == 'POST': cropper_master = Cropper(morph.master_image.path) cropper_slave = Cropper(morph.slave_image.path) master_cropdata = json.loads(request.POST['master']) slave_cropdata = json.loads(request.POST['slave']) cropper_master.crop(master_cropdata) cropper_slave.crop(slave_cropdata) # resize to the average of the two images mw, mh = cropper_master.image.size sw, sh = cropper_master.image.size w = (mw+sw)/2 h = (mh+sh)/2 print w, h cropper_master.resize(w, h) cropper_slave.resize(w, h) return redirect(label='morphin:points', args=[morph_id]) else: response = { 'morph': morph } return template_response('morphin/crop.html', response, request)
def generate(request, morph_id): try: morph = Morph.objects.get(id=morph_id) if morph.final: return redirect(label='morphin:view', args=[morph_id]) except: not_found() response = {} if request.method == 'POST' and 'data' in request.POST: response['status'] = 'ok' input = json.loads(request.POST['data']) morpher = Morpher(morph.master_image.path, morph.slave_image.path) gif_path = morpher.generate_frames(input['markers']) morph.morph_image = os.path.join('uploads', str(morph.id), os.path.basename(gif_path)) morph.points = json.dumps(input['markers']) morph.save() response['data'] = input response['image_url'] = morph.morph_image.url else: response['status'] = 'error' return json_response(response)
def points(request, morph_id): try: morph = Morph.objects.get(id=morph_id) if morph.final: return redirect(label='morphin:view', args=[morph_id]) except: not_found() response = {'morph': morph} return template_response('morphin/points.html', response, request)
def upload(request): response = {} if request.method == 'POST' and 'master' in request.FILES and 'slave' in request.FILES: master = request.FILES['master'] slave = request.FILES['slave'] morph = Morph() morph.save() try: w, h = 300, 300 morph.master_image.save("m_{0}".format(sanitize(master.name)), master, save=True) morph.slave_image.save("s_{0}".format(sanitize(slave.name)), slave, save=True) cropper_master = Cropper(morph.master_image.path) cropper_slave = Cropper(morph.slave_image.path) cropper_master.resize(w, h, even_if_larger=False) cropper_slave.resize(w, h, even_if_larger=False) return redirect(label='morphin:crop', args=[morph.id]) except Exception, e: morph.delete() return html_response(e)