使用 ImageMagick 将 SVG 裁剪并缩放为 PNG,无需像素化
我有一个包含许多多边形的 SVG:
https://github .com/barrycarter/bcapps/blob/master/sample-data/current-temps.svg
看起来有点像这样:
裁剪此图像并转换为 PNG 效果很好:
convert -crop 100x100+200+150 current-temps.svg /tmp/exhb2.png
但是,裁剪和缩放失败:
convert -crop 100x100+200+150 -scale 1000x750 current-temps.svg /tmp/exhb3.png
如何我做吗ImageMagick 在裁剪之前“放大”SVG?
(我意识到 ImageMagick 只能读取,不能写入 SVG 格式,但是 它应该仍然能够做我想做的事?)
编辑/解决:
谢谢,罗伯莫拉莱斯。
inkscape -z -e out4.png -w 1000 -h 1000 -a 200:200:400:400 current-temps.svg
(例如)就像魅力一样。
我还意识到复制 SVG、调整变换线:
<g transform="scale(3,3) rotate(-90) translate(-90,180)">
然后转换为 PNG 是另一种解决方案。
I have an SVG with many polygons:
https://github.com/barrycarter/bcapps/blob/master/sample-data/current-temps.svg
that looks somewhat like this:
Cropping this and converting to PNG works fine:
convert -crop 100x100+200+150 current-temps.svg /tmp/exhb2.png
Cropping and scaling, however, fails:
convert -crop 100x100+200+150 -scale 1000x750 current-temps.svg /tmp/exhb3.png
How do I make ImageMagick "zoom into" the SVG before cropping?
(I realize ImageMagick can only read, not write, the SVG format, but
it should still be able to do what I want?)
EDIT/SOLVED:
Thanks, robermorales.
inkscape -z -e out4.png -w 1000 -h 1000 -a 200:200:400:400 current-temps.svg
(for example) worked like a charm.
I also realized that copying the SVG, tweaking the transform line:
<g transform="scale(3,3) rotate(-90) translate(-90,180)">
and then converting to PNG is another solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在裁剪前进行缩放。
然而,使用 inkscape cli 来做到这一点更容易。
抱歉没有链接,afk
Try doing scale before crop.
However, doing that using inkscape cli is easier.
Sorry for no links, afk
不要将 SVG 裁剪为 PNG。
您可以使用 viewBox 重新定义裁剪区域,然后以尽可能最高的解决方案将该 SVG 转换为 PNG。
检查这篇文章 https://www.sarasoueidan.com/blog/svg-coordinate- Systems/ 解释一下什么是 viewBox,你就会明白我的想法。
Don't crop an SVG into PNG.
You can use viewBox to re-define the crop area then convert that SVG into PNG in highest solution as possible.
Check this post https://www.sarasoueidan.com/blog/svg-coordinate-systems/ explain what is viewBox and you will got my idea.
这里正确的 ImageMagick 语法是读取输入,然后裁剪,然后调整大小。请参阅http://www.imagemagick.org/Usage/basics/#why尽管这不太可能是问题所在。问题是 -scale 会复制像素,从而使其变得块状。您应该将 -scale 替换为 -resize。这会更平滑,但对于您所要求的放大倍数来说会变得模糊。尝试这个命令:
Proper ImageMagick syntax here is to read the input, then crop, then resize. See http://www.imagemagick.org/Usage/basics/#why Though that is not likely the issue. The issue is that -scale will replicated pixels and so make it blocky. You should replace -scale with -resize. That will be smoother, but blurry for the amount of magnification you are requesting. Try this command: