首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用flask将SVG下载为JPEG格式

使用flask将SVG下载为JPEG格式
EN

Stack Overflow用户
提问于 2016-10-01 09:20:51
回答 1查看 475关注 0票数 0

我正在尝试提供带有SVG属性的HTML页面,所以只要单击"Create“,我就希望能够以.jpg而不是SVG的形式下载该文件。我查看了多个与命令行配合工作的转换器,例如,如下所示。

代码语言:javascript
复制
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")

简单地说,我希望能够点击create并下载我的文件的.jpg版本。我目前正在尝试使用Flask来做这件事。

代码语言:javascript
复制
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>

因此,只要用户单击"create“,我就尝试下载(html)作为.jpeg。这是我的视图函数。

代码语言:javascript
复制
@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

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

EN

回答 1

Stack Overflow用户

发布于 2016-10-01 17:55:45

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

您可以直接从目录返回文件,这将为用户提供一个下载弹出窗口。

代码语言:javascript
复制
...
os.system("rsvg-convert -h 32 save.svg > icon-32.jpg")
return send_from_directory("/path/to/saved/image", "icon-32.jpg")

请确保导入方法,如下所示:

代码语言:javascript
复制
from flask import send_from_directory

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39802098

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档