图像处理主要依赖于qrcode库和PIL
python-qrcode是个用来生成二维码图片的第三方模块,依赖于 PIL 模块。
在实现的过程中一直出现路径错误,最后将保存的路径写完整后才正确,才能在页面显示。img.save('E:/python/kaifa/static/333.png')
一、主要的源代码:
kaifa.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from flask import Flask,render_template,redirect,url_for,request import qrcode from PIL import Image app = Flask(__name__) @app.route('/') def hello_world(): return redirect(url_for('url')) @app.route('/url',methods=['GET','POST']) def url(): if request.method == 'GET': return render_template('url.html') url = request.form['url'] img = qrcode.make(url) img.save('E:/python/kaifa/static/2.png') return render_template('img.html') if __name__ == '__main__': app.run(debug=True)
|
二、url.html文件源码:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>通过url生成二维码</title> </head> <body> <form action="/url" method="post"> url:<input type="text" name="url" value="http://"/> <input type="submit" value="生成二维码"/> </form> </body> </html>
|
三、img.html源码:
1 2 3 4 5 6 7 8 9 10
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>返回生成的二维码图片</title> </head> <body> <img src="/static/333.png" > </body> </html>
|
四、在本次实现需要注意的地方:

url_for()是找一个名叫url的路由函数,而不是去装饰器里找路由地址。所以/urls并不影响。

图中如果没有name参数,那么将获取不到用户输入的url,在与后端进行交互的时候只会识别name参数