RMagick的重采样方法调整图像大小,而不是重新采样(DPI分辨率不会改变) [英] RMagick's resample method resize the image, not resample it (the DPI resolution does not change)

查看:408
本文介绍了RMagick的重采样方法调整图像大小,而不是重新采样(DPI分辨率不会改变)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RMagick的 resample 方法来更改我拥有的图像的DPI分辨率(从 300x300 72x72

I'm using RMagick’s resample method to change the DPI resolution of an image I have (from 300x300 to 72x72.

我使用的代码:

original_image = Magick::Image.read("my300x300file.jpg") { self.density = "72.0x72.0" }
original_image.each do |image|
   image = image.resample(72.0, 72.0)
   image.density = "72x72"
   image.write("my72x72file.jpg") { self.quality = 50 }
end

执行代码后, my72x72file.jpg 尺寸已减少,但其 DPI分辨率仍然在 300 (这意味着文件大小尚未减少了很多;实际上,即使我将 self.quality 设置为 0 ,文件大小也不会更改)。

After executing the code, the my72x72file.jpg dimensions have been reduced, but its DPI resolution is still at 300 (which means the file size has not been reduced by much; in fact, even if I set the self.quality to 0, the file size pratically does not change).

推荐答案

更改文件的分辨率不会(也不应该)更改e文件大小。分辨率是每英寸的点数,而文件大小是根据实际像素的数量确定的(在控制其他所有内容之后,如文件大小,压缩等),无论它们消耗多少英寸。

Changing the resolution of your file doesn't (and shouldn't) change the file size. The resolution is the number of dots per inch, while file size is determined (after controlling for everything else like file size, compression, etc.) by the number of actual pixels, regardless of how many inches they are consuming.

如果你想改变图像的分辨率(即Photoshop,GIMP等人说的是,我发现的唯一有效的方法是创建一个具有适当分辨率的新图像,然后将原始图像合成到它上面。这似乎适得其反,但这是我完全可以使用它的唯一方法。

If you want to change the resolution of your image (i.e. what Photoshop, GIMP, etc. say it is), the only thing I've found that works is creating a new image with the proper resolution, then compositing your original onto it. This seems counterproductive, but it's the only way I could get it to work at all.

示例代码:

image = Magick::Image.read("my300x300file.jpg").first
dpi_image = Magick::Image.new(image.columns, image.rows) {
    self.density = "72x72"
    self.background_color = "none"
}
image = dpi_image.composite(image, Magick::CenterGravity, Magick::OverCompositeOp)

这篇关于RMagick的重采样方法调整图像大小,而不是重新采样(DPI分辨率不会改变)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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