【发布时间】: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