【问题标题】:Download SVG as JPEG using flask使用烧瓶将 SVG 下载为 JPEG
【发布时间】:2016-10-01 01:20:50
【问题描述】:

我正在尝试提供带有 SVG 属性的 HTML 页面;因此,只要单击“创建”,我就希望能够将该文件下载为 .jpg 而不是 SVG。例如,我查看了多个可与命令行一起使用的转换器。

os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")

类似,我希望能够单击创建并下载我的文件的 .jpg 版本。 我目前正在尝试使用 Flask 来做到这一点。

without_filter = <svg viewBox="0 0 400 150" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="background: #40484b;">
    <defs>
        <!-- if you use the same shape multiple times, you put the reference here and reference it with <use> -->
        <rect id="path-1" x="25" y="25" width="100" height="100"></rect>
        <rect id="path-3" x="150" y="25" width="100" height="100"></rect>
        <rect id="path-5" x="275" y="25" width="100" height="100"></rect>

        <!-- gradient -->
        <linearGradient x1="50%" y1="0%" x2="50%" y2="100%" id="gradient">
            <stop stop-color="#FF8D77" offset="0%"></stop>
            <stop stop-color="#FFBDB1" offset="50%"></stop>
            <stop stop-color="#F4E3F6" offset="100%"></stop>
        </linearGradient>

        <!-- clip-path -->
        <clipPath id="clip">
            <circle cx="325" cy="75" r="50"></circle>
        </clipPath>

        <!-- filter -->
        <filter x="-50%" y="-50%" width="200%" height="200%" filterUnits="objectBoundingBox" id="glow">
            <feGaussianBlur stdDeviation="15" in="SourceGraphic">
                <animate attributeName="stdDeviation" attributeType="XML"
                    begin="0s" dur="0.25s" repeatCount="indefinite"
                    values="15;20;15"/>
            </feGaussianBlur>
        </filter>
    </defs>
    <g fill-rule="evenodd">
        <use xlink:href="#path-1" filter="url(#glow)" fill="#FF8D77" fill-opacity="0.5"></use>
        <use xlink:href="#path-3" fill="url(#gradient)"></use>
        <use xlink:href="#path-5" fill="#FF8D77" clip-path="url(#clip)"></use>
    </g>
</svg>

因此,一旦用户单击“创建”,我就会尝试将 (html) 下载为 .jpeg 这是我的视图函数。

@app.route('/filters/<filename>', methods=['GET','POST'])
def AddFiltersTag(filename):
    form = AddFilterTags(request.form)
    if form.validate_on_submit():
        html = re.sub(r'(.*?<defs>)(.*)', r'\g<1>' + add_filter_tag + add_filter + end_tag + '\g<2>', without_filter)
        save_html = open('save.svg','wb')
        save_html.write(html)
        os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")
        return download_html

因此,当用户在我的表单中单击“提交”时,会触发 AddFilterTag 函数。但在那种情况下,我想下载,上面的代码只是将文件保存为 .svg 并稍后使用 os.system 进行转换,但我想直接下载 .jpg 版本。有没有办法做到这一点?

【问题讨论】:

  • 我自己没有尝试过,因为我不使用烧瓶,但是您可能想要使用 rsvg 库的 python 绑定。这样,您可以直接从“html”传递内容,而不是将它们写入 svg 文件以用作“rsvg-convert”命令中的参数。

标签: python flask operating-system jpeg encode


【解决方案1】:

保存文件并将其转换为图像后。

您可以直接从目录中返回文件,这会给用户一个下载弹出窗口。

...
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")
return send_from_directory("/path/to/saved/image", "icon-32.jpg")

一定要按如下方式导入方法:

from flask import send_from_directory

如果您在生产中使用它,请确保 nginx 或实际的 Web 服务器正在发送文件。你可以在http://flask.pocoo.org/docs/0.11/api/#flask.send_from_directory阅读更多内容

【讨论】:

  • 嘿,当我点击创建按钮时,它会抛出 404 NOT found。
  • 这是我的代码。save_html = open('static/save.svg','wb') save_html.write(html) transfer_file_name = 'icon-32.jpg' os.system("rsvg- convert -h "+height+""+filename+" > "+transfer_file_name) return send_from_directory(app.static_folder, "icon-32.jpg")
猜你喜欢
  • 1970-01-01
  • 2014-08-25
  • 2021-01-24
  • 1970-01-01
  • 2021-09-05
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多