转换SVG并将其调整为PNG [英] Convert and resize SVG to PNG

查看:69
本文介绍了转换SVG并将其调整为PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SVG转换为PNG(或任何栅格格式)并同时调整大小.

I am trying to convert SVG to PNG (or any raster format) and resize at the same time.

我以为我会使用ImageMagick来完成此任务,但是在调整大小之前,它似乎已转换为栅格.

I thought I would use ImageMagick for this task but it seems to be converting to raster before resizing.

这会导致图像质量较差.

This results in a poor quality image.

  • 有没有办法让ImageMagick在转换为栅格之前调整SVG的大小?

  • Is there a way to get ImageMagick to resize the SVG before converting to raster?

或者在调整大小后是否可以使用其他一些工具以编程方式将SVG转换为栅格?

Or is there some other tool I can use to programatically convert an SVG to a raster after resizing it?

或者,我还可以使用其他工具吗?

Alternatively, is there some other tool I could use for this?

当前我正在通过命令行使用ImageMagick:

Currently I'm using ImageMagick via a commandline:

convert file.svg -resize 100x100 file.png

直到运行时,源图像的大小"是未知的,目标大小是未知的.

The source image "size" is unknown and the destination size is not known until run-time.

推荐答案

这是我这样做的方法,并且似乎可行.

This is the way I do it and it seems to work.

convert -background none -density 1000 -resize 1000x compass.svg compass.png

这是每个部分的作用.

  • 使用 -background none 来确保SVG的任何透明部分保持透明,并且不会被白色填充.
  • 由于ImageMagick仅适用于光栅图像,因此您需要使用 -density 1000 并指定要调整SVG尺寸的宽度.这样,当您实际调用resize命令时,已加载的SVG的栅格表示形式将已经具有1000的宽度,否则最终将使SVG图像的原始大小改变大小.li>
  • 现在使用 -resize 1000x 为SVG提供新的宽度,系统会自动计算高度以保持宽高比.
  • Use -background none to make sure any transparent parts of the SVG stay transparent and not get filled with white.
  • As ImageMagick only works with raster images you need to use -density 1000 and specify the width to which you want to resize the SVG. This way when you actually call the resize command, the raster representation of the loaded SVG will already have a width of 1000, otherwise you'll end up resizing a raster up or down from whatever the original size of the SVG image is.
  • Now use -resize 1000x to give your SVG a new width, the height will be calculated automatically to keep the aspect ratio.

其中一个陷阱是,我真的不知道如何根据高度调整大小并计算宽度,因为将 -density 应用于宽度而不是高度.因此,您必须事先知道SVG的实际比例,并相应地使用宽度.

One pitfall of this, is that I don't really know how you could resize based on the height and let the width be calcualted since -density is applied to the width, not the height. So, you'd have to know the actual ratio of your SVG beforehand and work with the width accordingly.

这篇关于转换SVG并将其调整为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆