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 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 |
#coding =utf-8 from django.core.urlresolvers import reverse to django.shortcuts import Render_to_response, get_object_ or_404 from django.http import httpresponse,httpresponseredirect to django.template import RequestContext from Django.contrib Import messages Import os,uuid,imagefile,image from PhotoCut.headhat.forms import Photoform, Hatheadcutform from PhotoCut.headhat.models import Photo from photocut.settings import Media_root,headhat_abs_path, Headhat_vir_path def index (request,templates= "headhat/index.html"): template_var={} form=photoform () if request.method== "POST": Form = photoform (Request. Post.copy (), request. FILES) if Form.is_valid (): File=request. Files.get ("Photo_name", None) if File:p=imagefile.parser () for C in File.chunks (): P.feed (c) img=p.close () if Img.mode!= ' RGBA ': img = Img.convert (' RGBA ') if img.size[0]>img.size[1]: Offset=int (img.size[0]-img.size[1))/2 Img=img.crop ( Offset,0,int (Img.size[0]-offset), img.size[1]) Else:offset=int (img.size[1]-img.size[0))/2 IMG=img.crop ((0,offset,img.size[0], (Img.size[1]-offset)) Img.thumbnail (()) file_name= "%s.jpg"%str ( UUID.UUID1 ()) Img.save (Os.path.join (Headhat_abs_path,file_name), "JPEG", quality=100) messages.info (request,u "Upload success!") ") P=photo.objects.create (Photo_name=file_name) p.save () return httpresponseredirect reverse (" Headhat_cut ", kwargs= {"id":p. ID})) template_var["Form"]=form return Render_to_response (Templates,template_var,context_instance=requestcontext ( Request) def cut (request,id,templates= "headhat/cut.html"): template_var={} p=get_object_or_404 (Photo,pk=int (ID)) If not p.photo_name:messages.info (request,u "Please upload the picture First!") ") return Httpresponseredirect (Reverse (" Headhat_index ")) template_var[" Vir_path "]=os.path.join (Headhat_vir_path, P.photo_name) Form=hatheadcutform () if request.method== ' POST ': form=hatheadcutform (Request. POST) if Form.is_valid (): Try:img=image.open (Os.path.join (headhat_abs_path,p.photo_name)) except IOError: Messages.info (request,u read file Error!) ") Data=form.cleaned_data Img=img.crop (data["x1"],data["y1"],data["x2"],data["y2"]) Img.thumbnail (()) file_name= "%s_%s" (Os.path.splitext (P.photo _NAME) [0], "_50_50.jpg") Img.save (Os.path.join (Headhat_abs_path,file_name), "JPEG", quality=100) p.photo_thumb=file _name P.save () messages.info (request,u "cut success!") ") return Httpresponseredirect (Reverse (" headhat_show ")) Else:messages.info (request,u" Please cut and save! ") ") template_var[" form "]=form return Render_to_response (Templates,template_var,context_instance=requestcontext ( Request) def show (request,templates= "headhat/show.html"): template_var={} photos=photo.objects.all () template_var[ "Path"]=headhat_vir_path template_var["Photos"]=photos return Render_to_response (templates,template_var,context_ Instance=requestcontext (Request)) |