【问题标题】:Dynamically create and save image with Django and PIL/Django-Photologue使用 Django 和 PIL/Django-Photologue 动态创建和保存图像
【发布时间】:2011-01-28 16:10:02
【问题描述】:

我想生成一个包含动态内容的 html 页面,然后将结果保存为图像并将页面显示给用户。因此,用户注册参加会议并提供他们的姓名和组织。该数据与 html/css 元素相结合,以显示他们的会议 ID 徽章的外观(他们的姓名和组织在我们的会议徽标背景之上)以供预览。经批准后,该页面会以图像格式(PNG、PDF 或 JPG)保存在服务器上,以便管理员稍后将其打印到物理徽章上。我正在使用由 PIL 提供支持的 Django 和 django-photologue。

视图可能如下所示

# app/views.py

def badgepreview(request, attendee_id):
    u = User.objects.get(id=attendee_id)
    name = u.name
    org = u.org

    return render_to_response('app/badgepreview.html',
        {'name':name,'org':org,},
        context_instance = RequestContext(request),
    )

模板可能如下所示

{# templates/app/badgepreview.html #}
{% extends "base.html" %}
{% block page_body %}
    <div style='background:url(/site_media/img/logo_bg.png) no-repeat;'>
        <h4>{{ name }}</h4>
        <h4>{{ org }}</h4>
    </div>
{% endblock %}

很简单,但是如何保存结果呢?还是有更好的方法来完成这项工作?

【问题讨论】:

  • 您是否考虑过仅使用 PIL 动态生成图像?

标签: django image-processing python-imaging-library photologue


【解决方案1】:

我唯一能想到的就是分两次完成:

a) 使用http://www.xhtml2pdf.com/ 将 HTML 转换为 PDF。

b) 使用 http://www.swftools.org/gfx_tutorial.html 之类的东西将 PDF 转换为图像。

我无法想象这样做会很快......

您最好只转换并允许他们下载 PDF(即仅使用上面的步骤 a))或尝试直接生成徽章而无需 HTML 中间步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-15
    • 2014-12-27
    • 2011-01-07
    • 2021-11-01
    • 1970-01-01
    • 2013-11-03
    • 2013-01-18
    • 2014-07-18
    相关资源
    最近更新 更多