Tornado框架的图片上传和保存操作(八)

2020-04-15  本文已影响0人  梦捷者

简单图片上传和文件保存的操作(没有用到数据库)

1、self.request.files 的使用和Python 操作文件,写入数据(open的使用)

class UploadHandler(BaseHandler):

    def get(self):
        self.render('9upload.html')

    def post(self):
        try:
            files = self.request.files['picture']#list类型中包含一个字典
            if files[0]:
                dict_img = files[0]
                filename = dict_img['filename']
                print(filename)
                print(dict_img['content_type'])
                with open('static/upload/{}'.format(filename), 'wb') as f:
                    f.write(dict_img['body'])
                self.render('10upload_success.html', filename=files[0]['filename'])
            else:
                self.write("上传失败,系统不听话")
        except:
            self.write("上传失败,上传的图片不可以为空")
 handlers = [
        (r'/upload', UploadHandler)
    ]
{% extends base.html %}
{% block title %}
上传图片界面
{% end %}
{% block  body%}
<form action="/upload" enctype="multipart/form-data" method="post">
   上传文件: <input type="file" name="picture"/><br>
    <input type="submit"/>
</form>
{% end %}
{% extends base.html %}
{% block title %}
图片上传成功界面
{% end %}

{% block body%}
你成功上传了图片{{ filename }}!!!!,图片如下:<br>
<img src="{{ static_url('upload/{}'.format(filename)) }}" width="250" height="250">
{% end %}

2、用简单的目录检索来展示图片(使用 Python 标准库 glob)

from glob import glob
list_imgs = glob('static/upload/*')
new_list_img =[]
for list_img in list_imgs:
    print(list_img)
    new1 = list_img.replace('static/upload/', 'upload/')
    new_list_img.append(new1)
from upload_liat import new_list_img
class UploadHandler(BaseHandler):

    def get(self):
        self.render('9upload.html')

    def post(self):
        try:
            files = self.request.files['picture']#list类型中包含一个字典
            if files[0]:
                dict_img = files[0]
                filename = dict_img['filename']
                print(filename)
                print(dict_img['content_type'])
                with open('static/upload/{}'.format(filename), 'wb') as f:
                    f.write(dict_img['body'])

                self.render('10upload_success.html', filename=files[0]['filename'], list_img=new_list_img)
            else:
                self.write("上传失败,系统不听话")
        except:
            self.write("上传失败,上传的图片不可以为空")
{% extends base.html %}
{% block title %}
图片上传成功界面
{% end %}

{% block body%}
你成功上传了图片{{ filename }}!!!!,图片如下:<br>
<img src="{{ static_url('upload/{}'.format(filename)) }}" width="250" height="250"><br>
{% set line="===="*6 %}
{{ line }}<br>
其他的上传图片还有如下几张:<br>
{% for img in list_img %}
<img src="{{ static_url(img) }}" width="250" height="250">
{% end %}
{% end %}

3、缩略图的生成

pip install pillow
from PIL import Image
class UploadHandler(BaseHandler):

    def get(self):
        self.render('9upload.html')

    def post(self):
        try:
            files = self.request.files['picture']#list类型中包含一个字典
            if files[0]:
                dict_img = files[0]
                filename = dict_img['filename']
                print(filename)
                print(dict_img['content_type'])
                save_path = 'static/upload/{}'.format(filename)
                with open(save_path, 'wb') as f:
                    f.write(dict_img['body'])
                im = Image.open(save_path)
                im.thumbnail((200, 200))
                im.save('static/thumbs/thumb_{}'.format(filename))
                self.render('10upload_success.html', filename=files[0]['filename'], list_img=new_list_img, new_list_thumb=new_list_thumb)
            else:
                self.write("上传失败,系统不听话")
        except Exception as e:
            print(e)
            self.write("上传失败,上传的图片不可以为空")
from PIL import Image
import glob, os

imgs = glob.glob('*.jpg')
for img in imgs:
    name, ext = os.path.splitext(img)
    im = Image.open(img)
    im.thumbnail((200, 200))
    im.save("{}_{}x{}.jpg".format(name, 200, 200), "JPEG")
上一篇 下一篇

猜你喜欢

热点阅读