使用 rmagick 调整 ruby on Rails 中的图像大小而不损失质量
我需要在 rmagick 中将图像的大小从当前大小调整为固定宽度到 300 的宽度,而不损失图像质量。目前我正在对 jpg 图像执行以下操作
如果previewImage.columns > 300 PreviewImage.change_geometry!("300x") { |列、行、img| img.resize!(列,行) } 结尾
我不确定这是否是最好的方法,因为我在生成的图像中失去了清晰度。我希望能够通过损失最少的图像质量来调整大小。
我知道有过滤器和压缩技术: JPG JPEG压缩、LosslessJPEG压缩
,但不知道哪个能提供最佳结果,或者我可以将图像转换为 png 然后调整大小吗?
I need to resize an image in rmagick from its current size to a fixed width to a width of 300 without losing image quality. Currently I am doing following for a jpg image
if previewImage.columns > 300 previewImage.change_geometry!("300x") { |cols, rows, img| img.resize!(cols, rows) } end
am not sure if this the best way as I am losing sharpness in the resulting image. I want to be able to resize by losing the least amount of image quality.
I know there are filters and compression techniques:
JPG JPEGCompression, LosslessJPEGCompression
but don't know which would provide the best result or could i convert the image to png and then resize?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
转换为 PNG 是无损的。调整大小总是意味着质量上的一些损失(您可以尝试各种图像魔术调整大小过滤器,看看哪一种最适合您的图像......确实没有“最好”的过滤器,这就是为什么他们让您选择。如果图像虽然你处理的图像通常是相同的,但你应该能够通过试验来选择最佳的图像,从 PNG 转换回 JPG 会意味着更多的损失,所以如果你确实走那条路,我会将图像保留为 PNG。
The conversion to PNG would be lossless. Resizing will always imply some loss in quality (you can try out the various image magick resize filters to see which one is best for your images... there really is no "best" one, that's why they let you choose. If the images you're dealing with are generally the same though, you should be able to pick an optimal one just by experimenting. Conversion from PNG back to JPG would imply more loss, so I would keep the image at PNG if you do go that route.