惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
P
Privacy International News Feed
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CXSECURITY Database RSS Feed - CXSecurity.com
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Attack and Defense Labs
Attack and Defense Labs
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
GbyAI
GbyAI
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Y
Y Combinator Blog
C
CERT Recently Published Vulnerability Notes
N
Netflix TechBlog - Medium
S
Security Affairs
Spread Privacy
Spread Privacy
罗磊的独立博客
腾讯CDC
MyScale Blog
MyScale Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 热门话题
The Cloudflare Blog
L
LangChain Blog
博客园_首页
H
Hacker News: Front Page
宝玉的分享
宝玉的分享
Martin Fowler
Martin Fowler
博客园 - 聂微东
SecWiki News
SecWiki News
A
Arctic Wolf
爱范儿
爱范儿
Google Online Security Blog
Google Online Security Blog
T
Threat Research - Cisco Blogs
Hacker News - Newest:
Hacker News - Newest: "LLM"
有赞技术团队
有赞技术团队
The GitHub Blog
The GitHub Blog
Cyberwarzone
Cyberwarzone
博客园 - 叶小钗
V
Visual Studio Blog
V
V2EX
T
Tailwind CSS Blog
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
MongoDB | Blog
MongoDB | Blog
D
Docker

Sehnsucht

记一次博客换图床的过程 观《花束般的恋爱》 读《活过》 东京 旅行篇 亲人逝去 我为什么想养鱼 防止AI爬取你的博客 「monthly 」博客重构 游戏全成就 好看的小说 人生的每一步都不会是浪费-2024年终 「weekly」星期五综合征 「weekly」听播客 杂谈 读《美丽新世界》 「weekly」社交媒体 恶性事件 新世界 「weekly」认知觉醒 美丽新世界 follow 「weekly」独立博客9问题 双十一买书 读《惊呆了!原来这就是社会学》 一次聊天与自我建设 2024-09月记 补番《relife》 树状数组简单理解 lvm简单使用 博客自动发布方案 某公众号废案 fail2ban基本使用 Podman 环境使用 Nginx Proxy Manager 最佳实践 2024-07同学聚会 vps使用podman部署freshrss Gitlab CICD 实践,思考与记录 装备升级:新的PC gitlab局域网搭建流程 FastMail迁移至GMail+Cloudflare Email Routing 读《黑客与画家》 leetcode 第 391 场周赛 Astro添加过渡后DarkModeToggle按钮失效 nuxt3使用echarts5渲染中国地图 人生刻度 又到凤凰花朵开放的时候 2023-05-10 实习结束 2023-04-26 广州博物馆游记 YOASOBI「たぶん」官方音乐视频 风灵玉秀-发如雪 带我的老哥离职了 从p10k转向Starship lunarvim国内安装踩坑记录 什么是对称加密非对称加密、密钥交换、数字签名、证书 强调体验的一种思考时间的方式 ArchLinux使用vscode编写latex报错The font "FontAwesome" cannot be found ArchLinux安装(移动硬盘)流程 yay一个或多个文件没有通过有效性检查! 2022年终 hugo建站伊始 clash设置relay前置代理(校园网破解改进记录) 群青 · YOASOBI · Ayase 华为 matepad pro 11使用体验 Nginx使用acme.sh免费安装ssl证书 永远属于你的安娜 在线 童年趣事 童心,是比野心更难得的梦想 2021年终总结 基于jeeSite的软件测试课程作业 流程记录 Linux不同用户安装不同版本jdk Wifi4更换Wifi6路由器的使用体验 玩客云刷机debain个人记录 jython 简单入门 Jenkins 个人搭建流程记录 集成邮件系统(qq邮箱),gitlab服务器,freestyle风格 在Web项目中配置Log4j --指南-- Codeforces Round 753 (Div. 3) ABCDE 浅谈fork函数 JavaWeb servlet 使用Cookie记录用户访问次数 Codeforces Round Educational Codeforces Round 114 (Rated for Div. 2) ABC 2021牛客多校7 I xay loves or 2021牛客多校9 H Happy Number 2021牛客多校8 D OR 2021牛客多校5 B Boxes 2021牛客多校5 H Holding Two 2021牛客多校2 D Er Ba Game 2021牛客多校1 F Find 3-friendly Integers 2021牛客多校1 B Ball Dropping 记人生第一次投简历和笔试 原型模式 2021-2-20 雨时随记 关于一维差分数组的例子 halo个人建站 关于手机ping电脑和电脑ping手机 泛型类简单理解 CodeForces - 849C From Y to Y 2020牛客国庆集训派对day3 Leftbest 约瑟夫环问题---2020牛客国庆集训派对day2 AKU NEGARAKU mysql 5.7 忘记密码如何更改 CF 1099B Squares and Segments CF 998A Balloons CF 998B Cutting 链表应用之多项式相加 CF 1150A Stock Arbitraging CF 1199A City Day CF 1199B Water Lily CF 982A Row CF 934A A Compatible Pair CF 629B Far Relative’s Problem 651A Joysticks 651B Beautiful Paintings
Flask 上传图片并灰度显示
2021-12-06 · via Sehnsucht

接到了一个小需求:使用flask写上传图片并灰度显示

刚好没写过python的后端,来试一试

在这里插入图片描述

代码

使用了layui的组件

app.js

import logging
import os

from flask import Flask, render_template, request
from PIL import Image

app = Flask(__name__)

# 映射到index.html
@app.route('/')
def index():
    return render_template('index.html')

# 指定post请求
@app.route('/upload', methods=['post'])
def upload():
    # 文件不存在就退出
    if ('file' not in request.files):
        print('no found file')
        return
    img = request.files['file']

    path = os.path.abspath(os.path.dirname(__file__)) + "/static/photo"
    
	# 日志打印
    app.logger.info("path: " + path)
	# 创建文件夹
    if os.path.exists(path) == False:
        os.makedirs(path)
	
    file_path = path + "/" + img.filename
    app.logger.info('file_path: ' + file_path)
	# 保存图片
    img.save(file_path)

    img2 = Image.open(file_path)
	# 灰度变化
    Img = img2.convert('L')
    Img.save(file_path)

    fname = '/static/photo/' + img.filename

    app.logger.info(fname)

    return render_template('view.html', fpath=fname)

if __name__ == '__main__':
    app.run()

# todo 可以增加的功能,指定上传类型,生成唯一图片标识符防止图片名重复

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css">
    <script src="/static/layui/layui.js"></script>
    <script src="/static/js/jquery-3.6.0.js"></script>
    <style>
        body {
            text-align: center;
            padding: 20px;
        }

        form {
            width: 500px;
            margin: auto;
        }

    </style>
</head>
<body>

<form method="post" action="/upload" enctype="multipart/form-data" class="layui-form">
    <div class="layui-form-item wu-example" id="uploader" style="display: inline-block;">
        <div class="layui-input-block" >
            <input type="file" name="file"  class="layui-btn" value="添加文件"/><button class="layui-btn" lay-submit lay-filter="*">上传</button>
        </div>
    </div>
</form>

<script>

</script>

</body>
</html>

view.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>图片显示</title>
    <style>
        body {
            text-align: center;
            padding: 20px;
        }
    </style>
</head>
<body>
    <img src="{{ fpath }}" style="height: 300px;width: 500px" alt="pic">
</body>
</html>

部署

使用了gunicorn部署flask

在阿里云上安装pyinstaller,进行打包,然后安装gunicorn(有很多依赖组件)

将文件上传到云服务器

安装完成后输入

gunicorn  -w 2 -b :8080 app:app

这里我踩坑了,理论上gunicorn -w 2 -b 127.0.0.1:8080 app:app也是可以的,但是我那个版本好像不行,然后后面那个app:app,前面那个是你运行的文件名,后面那个是你Flask的对象,就像我app.py(前一个app)中的app = Flask(__name__)

然后正常访问即可

参考:

gunicorn文档

Flask文档