一例tornado框架下处理上传图片并生成缩略图的例子

class coachpic(RequestHandler):
    @gen.coroutine
    def post(self):
        picurl = self.request.files['picurl'][0]
        print("picurl:{}".format(picurl))
        ret = yield self.editpic(picurl)
        self.write(ret)
    @gen.coroutine
    def editpic(self,picfile):
        filename = picfile["filename"]
        current_path = os.getcwd()
        print("cwd:{}".format(current_path))
        originpath = current_path + "/templates/static/pics/coach/"
        if os.path.exists(originpath) == False:
            os.mkdir(originpath)
        with open( originpath+ filename, "wb") as picwriter:
            picwriter.write(picfile["body"])
        saiwa = Image.open(originpath+ filename)
        width = saiwa.size[0]
        height = saiwa.size[1]
        portion = 200 / height
        width = round(width * portion, 2)
        height = round(height * portion, 2)
        saiwa.thumbnail((width, height))
        thpath = os.getcwd() + "/templates/static/thumbnail/coach/"
        if os.path.exists(thpath) == False:
            os.mkdir(thpath)
        saiwa.save(thpath + "th_" + filename)
        print("宽{},高{}".format(width, height))
        originsrc = "/static/pics/coach/" + filename
        subsrc = "/static/thumbnail/coach/" + "th_" + filename
        raise Return({"origin": originsrc, "subsrc": subsrc})

 

转载于:https://www.cnblogs.com/saintdingspage/p/10952774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值