python - 使用谷歌应用程序引擎渲染动态图像

标签 python google-app-engine

我正在开发一个 Google 应用引擎项目,我在数据存储区中保存了一些图像。 现在我必须调整这些图像的大小并将它们渲染到模板。我已成功获取图像,但我不知道如何使用其他数据字典将它们渲染到模板中。我正在将应用程序引擎补丁与 GAE 结合使用。

以下是我在 View 中使用的代码片段:

def getData(request,key):
forum = Topic.get(key)
picData = forum.creator.portfolio_set
response = ''
if picData:
    picture = picData[0].userpic
    response = HttpResponse(picture, mimetype="image/jpeg")
    #response['Content-Type'] = 'image/jpeg'
    response['Content-Disposition'] = 'inline'
return render_to_response(request,"forum.html",{"forum":forum,"pic":response })

现在,我能够渲染论坛变量中的数据,但不知道如何渲染与其关联的图像。我应该在 View 文件中修改什么以及应该在模板中使用什么来显示 View 文件渲染的图像。

请提出建议。

提前致谢。

最佳答案

解决方案1:

添加通用 View 以获取图像,例如/images/image-id 应返回该 ID 的图像

def get_image(request, image_id): ....

picture = get from id
return HttpResponse(picture, mimetype="image/jpeg")

现在在 forum.html 模板中使用图像 URL,就像使用任何普通图像 URL 一样。

解决方案 2:您可以使用 data:image 直接嵌入图像,请参阅 http://en.wikipedia.org/wiki/Data_URI_scheme

所以试试这个

import base64

def getData(request,key):
    forum = Topic.get(key)
    picData = forum.creator.portfolio_set
    pictureSrc = ''
    if picData:
        picture = picData[0].userpic
        pictureSrc = "data:image;base64,%s"%base64.b64encode(picture)

    return render_to_response(request,"forum.html",{"forum":forum,"pic":pictureSrc })

关于python - 使用谷歌应用程序引擎渲染动态图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3245451/

相关文章:

python - 如何导入包含具有循环依赖的类的模块?

r - 错误 : (gcloud. app.deploy)使用自定义运行时时必须提供自己的 Dockerfile

java - 使用 appengine-gcs-client-0.5 dev_appserver 存储文件的 InvocationTargetException

php - 我可以在 Google App Engine for Python 上运行 PHP 文件吗?

python - 基于 python 的 PowerShell?

python - 如何键入注释字符串的长度?

python - 使用 Paramiko 在远程计算机上通过 sudo 执行 SFTP 操作

python - 应用程序在源代码后不起作用

php - GAE PHP 应用程序 : Unable to find the wrapper "gs"

maven - mvn 应用引擎 :update will not deploy due to permissions error