如何使用python';烧瓶上的QRS码

如何使用python';烧瓶上的QRS码,python,flask,Python,Flask,我有一个生成QR图像的函数: import qrcode def generateRandomQR(): qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data("Huehue") qr.make(

我有一个生成QR图像的函数:

import qrcode
def generateRandomQR():
    qr = qrcode.QRCode(version=1,
            error_correction=qrcode.constants.ERROR_CORRECT_L,
            box_size=10,
            border=4,
            )

    qr.add_data("Huehue")
    qr.make(fit=True)
    img = qr.make_image()
    return img
现在的想法是生成图像,然后将其扔到烧瓶上,作为图像,这是我的烧瓶功能:

@app.route("/qrgenerator/image.jpg")
def generateQRImage():
    response = make_response(qrWrapper.generateRandomQR())
    response.headers["Content-Type"] = "image/jpeg"
    response.headers["Content-Disposition"] = "attachment; filename=image.jpg"
    return response

但它似乎工作不正常。。。我犯了500个错误,所以我不太确定我错了什么

编辑:在回答问题后看到您关于不想保存到临时文件的评论。但是,如果您决定将其保存到临时位置,这里有一种方法

您可以将二维码图像保存在临时位置,并使用
send\u file
提供二维码图像

发送文件
记录在

我还没有测试过这个代码片段,但是类似的东西应该可以工作

from flask import send_file

@app.route("/qrgenerator/image.jpg")
def generateQRImage():
    response = make_response(qrWrapper.generateRandomQR())

    temp_location = '/tmp/image.jpg'

    # Save the qr image in a temp location
    image_file = open(temp_location, 'wb')
    image_file.write(response)
    image_file.close

    # Construct response now
    response.headers["Content-Type"] = "image/jpeg"
    response.headers["Content-Disposition"] = "attachment; filename=image.jpg"
    return send_file(temp_location)

Google+上的一位先生为我提供了一个解决方案,尽管他没有SO帐户,但我决定分享他的答案:

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
"""Example of Flask and qrcode.

NOTE: by requirements image in memory!
"""

__author__ = 'Daniel Leybovich <setarckos@gmail.com>'
__version__ = (0, 0, 1)


import os
import sys
import flask
import qrcode
import cStringIO


app = flask.Flask(__name__)


def random_qr(url='www.google.com'):
    qr = qrcode.QRCode(version=1,
                       error_correction=qrcode.constants.ERROR_CORRECT_L,
                       box_size=10,
                       border=4)

    qr.add_data(url)
    qr.make(fit=True)
    img = qr.make_image()
    return img


@app.route('/get_qrimg')
def get_qrimg():
    img_buf = cStringIO.StringIO()
    img = random_qr(url='www.python.org')
    img.save(img_buf)
    img_buf.seek(0)
    return flask.send_file(img_buf, mimetype='image/png')


if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)
#/usr/bin/env python2.7
#-*-编码:utf-8-*-
“”“烧瓶和qrcode的示例。
注:按要求将图像存储在内存中!
"""
__作者:丹尼尔·莱博维奇
__版本=(0,0,1)
导入操作系统
导入系统
进口烧瓶
导入QR码
导入cStringIO
app=烧瓶。烧瓶(\uuuuu名称\uuuuuuu)
def random_qr(url='www.google.com'):
qr=qr码。qr码(版本=1,
error\u correction=qrcode.constants.error\u CORRECT\L,
盒子尺寸=10,
边框=4)
qr.添加_数据(url)
qr.make(拟合=真实)
img=qr.make_image()
返回img
@应用程序路径('/get\u qrimg')
def get_qrimg():
img_buf=cStringIO.StringIO()
img=random_qr(url='www.python.org')
img.save(img_buf)
img_buf.seek(0)
返回烧瓶。发送文件(img\u buf,mimetype='image/png')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(host='0.0.0.0',debug=True)

您是否尝试在调试模式下运行以查看实际错误是什么?我没有,但我怀疑img qr.make_image()返回的是python图像对象,而不是实际的二进制文件。如果是这样的话。。。我可以保存它,然后再次打开该文件进行阅读,然后提供它。但如果可能的话,我想避免这种“暗淡”