使用 RMagick 读取图像时设置密度

发布于 2024-11-12 03:06:19 字数 860 浏览 3 评论 0原文

我正在尝试使用 RMagick 将 SVG 转换为不同大小的 PNG。

当我使用Magick::Image.read('drawing.svg')读取SVG并将其写入drawing.png时(相当于运行convert Drawing.svg绘图。 png 来自命令行),大小为 744x1052。

假设我希望 PNG 是默认大小的两倍。您不能只是读入它,调整它的大小,然后将其写出,因为这首先对 SVG 进行光栅化,然后将该图像缩放到原来的两倍,从而失去质量和首先使用矢量图形的全部好处。因此,如果我理解正确,您应该在读取时设置图像的密度。

image = Magick::Image.read('drawing.svg'){self.density = 144}.first

但是 image.densis 仍然将密度报告为“72x72”,如果我写出图像,它的大小与以前相同,即 744x1052。我在读取时如何指定密度似乎并不重要。对于 144、“144”、144.0、“144.0”、“144x144”和“144.0x144.0”,它总是返回“72x72”。

从命令行运行 convert -密度 144 Drawing.svg Drawing.png 会按预期工作,并生成一个比以前大两倍的 PNG (2104x1488)。

我使用的是 OS X 10.6.7、ImageMagick 6.7.0-0(通过 MacPorts 安装)、RMagick 2.13.1 和 Ruby 1.9.2p180。当我将代码放入 Heroku 上的一个小型 Sinatra web 应用程序的上下文中时,它具有相同的错误行为,因此问题似乎不在于 OS X 或 MacPorts。

I am attempting to use RMagick to convert an SVG to a PNG of a different size.

When I read in the SVG with Magick::Image.read('drawing.svg') and write it out to drawing.png (the equivalent of just running convert drawing.svg drawing.png from the command line), the size is 744x1052.

Let's suppose I want the PNG to be twice as large as it is by default. You can't just read it in, resize it, then write it out, as that first rasterizes the SVG and then scales that image to be twice as large, losing quality and the entire benefit of using a vector graphic in the first place. So instead, if I understand correctly, you're supposed to set the image's density upon read.

image = Magick::Image.read('drawing.svg'){self.density = 144}.first

But image.density still reports the density as "72x72", and if I write out the image it has the same size as before, 744x1052. It doesn't seem to matter how I specify the density upon read. With 144, "144", 144.0, "144.0", "144x144", and "144.0x144.0", it always comes back "72x72".

Running convert -density 144 drawing.svg drawing.png from the command line works as expected and generates a PNG that's twice as large as before, 2104x1488.

I'm using OS X 10.6.7, ImageMagick 6.7.0-0 (installed via MacPorts), RMagick 2.13.1, and Ruby 1.9.2p180. When I put my code into the context of a little Sinatra webapp on Heroku, it has the same incorrect behavior, so the issue does not seem to lie with OS X or MacPorts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-11-19 03:06:19

密度与分辨率(即每英寸点数)有关,而不是渲染的尺寸。来自精细手册

图像的垂直和水平分辨率(以像素为单位)。默认值为“72x72”。

我认为您正在寻找 resize调整大小!

将接收器的尺寸更改为指定尺寸。

您可以通过两种方式指定新大小。明确指定新的宽度和高度,或指定比例因子(表示百分比变化的数字)。

所以这会起作用:

Magick::Image.read('drawing.svg').first.resize(2).write('drawing.png')

或者这样:

img = Magick::Image.read('drawing.svg').first
img.resize!(2)
img.write('drawing.png')

我不知道为什么 convert 的行为与库不同,可能有其他默认设置在库中具有不同的默认值,或者可能是 -密度 的作用不仅仅是设置密度。

如果 resize 没有为您解决问题(并且根据您的评论,它发生得太晚而无法使用),您可以尝试设置 块中的size参数

img = Magick::Image.read('drawing.svg'){ |opts| opts.size = '2104x1488' }.first

当然,你必须事先知道SVG有多大。您应该能够为几何图形指定诸如 200%x200% 之类的内容,但 read 始终忽略 Magick::Geometry 当我尝试时。

Density is about resolution (i.e. dots per inch), not the rendered size. From the fine manual:

The vertical and horizontal resolution in pixels of the image. The default is "72x72".

I think you're looking for resize or resize!:

Changes the size of the receiver to the specified dimensions.

You can specify the new size in two ways. Either specify the new width and height explicitly, or specify a scale factor, a number that represents the percentage change.

So this will work:

Magick::Image.read('drawing.svg').first.resize(2).write('drawing.png')

Or this:

img = Magick::Image.read('drawing.svg').first
img.resize!(2)
img.write('drawing.png')

I don't know why convert behaves differently than the library, there could be other default settings in play that have different defaults in the library or maybe -density does more than set the density.

If resize isn't doing the trick for you (and, based on your comments, it is happening too late to be of use), you can try setting the size parameter in the block:

img = Magick::Image.read('drawing.svg'){ |opts| opts.size = '2104x1488' }.first

Of course, you have to know how big the SVG is before hand. You're supposed to be able to specify things like 200%x200% for the geometry but read always ignores the flag on the Magick::Geometry when I try it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文