生成金字塔视图的二维码,无需写入到显示器中返回

2024-10-01 22:32:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个金字塔视图,需要生成一个二维码并将其作为图像返回给用户。我想避免存储图像,我只想生成它,发送它,然后忘掉它。在

我做的第一件事是这样的:

oRet = StringIO.StringIO()
oQR  = pyqrcode.create('yo mamma')
oQR.svg(oRet, scale=8)
return Response(body = oRet.read(), content_type="image/svg")

但这会生成一个无法打开的svg文件。在

再近一点看:

^{pr2}$

所以我的问题是:如何将新生成的二维码作为金字塔响应返回,而不将其存储在磁盘上?我不太在意图像的格式


Tags: 用户svg图像视图returnresponsecreateyo
2条回答

这是我的代码:

from pyramid.config import Configurator
from pyramid.response import Response
from pyramid.response import FileResponse
from pyramid.view import view_config
from io import StringIO
from io import BytesIO
import matplotlib.pyplot as plt
import numpy as np
import pyqrcode

 @view_config(route_name='qrcview')
def qrc_test2(request):
    oRet = BytesIO()
    oQR  = pyqrcode.create('bla bla bla bla bla ')
    oQR.png(oRet, scale=10)
    print(type(oRet.getvalue()))
    response =  Response(body = oRet.getvalue(), 
    content_type="image/png")
    return response

 if __name__ == '__main__':
      with Configurator() as config:
          config.add_route('qrcview', '/qrc2')
          config.scan('__main__')
          app = config.make_wsgi_app()
server = make_server('0.0.0.0', 6543, app)
server.serve_forever()

我们有一个赢家:

oRet = StringIO.StringIO()
oQR  = pyqrcode.create('yo mamma')
oQR.png(oRet, scale=8)

oResp =  Response(body = oRet.getvalue(), content_type="image/png",content_disposition='attachment; filename="yummy.png"')
return oResp

诀窍是用oRet.getvalue()代替oRet.read()

相关问题 更多 >

    热门问题