zoukankan      html  css  js  c++  java
  • 一例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})
  • 相关阅读:
    springcloud概述
    springcloud-微服务架构基础
    TypeScript 教程
    提示工具以及弹出框
    Bootstrap 弹出框(Popover)插件
    JavaScript JSON
    JavaScript常见基础函数
    7种JavaScript代码调试的方法
    Bootstrap 网格系统
    文本元素
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/10952774.html
Copyright © 2011-2022 走看看