圆月山庄资源网 Design By www.vgjia.com
本文实例讲述了Django imgareaselect手动剪切头像的方法。分享给大家供大家参考。具体如下:
index.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>上传图片</title> </head> <body> <form action="." method="post" enctype="multipart/form-data">{% csrf_token %} <table border="0"> {{form.as_table}} <tr> <td></td> <td><input type="submit" value="上传"/></td> </tr> </table> </form> </body> </html>
show.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HTML5的标题</title> <style> ul {width:80%;padding:5px;} li{list-style:none;float:left;padding:5px;margin:5px;background-color:#ccc;} .info{color:green;} </style> </head> <body> <p><a href="{%url headhat_index%}">继续上传头像</a></p> {% if messages %} {% for message in messages %} <p{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</p> {% endfor %} {% endif %} <ul> {%for p in photos%} <li><img src="/UploadFiles/2021-04-08/{{path}}{{p.photo_name}}">cut.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>剪切</title> <link rel="stylesheet" type="text/css" href="/static/jquery.imgareaselect-0.9.3/css/imgareaselect-default.css" /> <style rel="stylesheet" type="text/css" > .area { background:none repeat scroll 0 0 #EEEEFF; border:2px solid #DDDDEE; padding:0.6em 0.6em 1em 0.6em; width:85%; display:block; margin-bottom:1em; } div.frame { background:none repeat scroll 0 0 #FFFFFF; border:2px solid #DDDDDD; padding:0.4em; } .info{color:green;} </style> <script type="text/javascript" src="/UploadFiles/2021-04-08/jquery.min.js">forms.py:
#coding=utf-8 from django import forms class PhotoForm(forms.Form): photo_name = forms.ImageField(label=u"头像") class HatHeadCutForm(forms.Form): x1=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,})) y1=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,})) x2=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,})) y2=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,})) w=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,})) h=forms.IntegerField(widget=forms.TextInput(attrs={'size': 4,}))models.py:
#coding=utf-8 from django.db import models class Photo(models.Model): photo_name=models.CharField(u"图片路径",max_length=255) photo_thumb=models.CharField(u"图片缩略图",max_length=255)views.py:
#coding=utf-8 from django.core.urlresolvers import reverse from django.shortcuts import render_to_response, get_object_or_404 from django.http import HttpResponse,HttpResponseRedirect from 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((300, 300)) file_name="%s.jpg"%str(uuid.uuid1()) img.save(os.path.join(HEADHAT_ABS_PATH,file_name),"JPEG",quality=100) messages.info(request,u"上传成功!") 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"请先上传图片!") 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"读取文件错误!") data=form.cleaned_data img=img.crop((data["x1"],data["y1"],data["x2"],data["y2"])) img.thumbnail((50, 50)) 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"剪切成功!") return HttpResponseRedirect(reverse("headhat_show")) else: messages.info(request,u"请剪切后 再保存!") 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))希望本文所述对大家的Python程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]