RMagick:缩放图像并调整缩略图大小

发布于 2024-10-11 16:59:56 字数 257 浏览 13 评论 0原文

我想调整/缩放图像。原件的尺寸与 300x200 或 512x600 不同。我想将图像大小调整为 100x100,但不要裁剪图像中的任何内容或更改比例。理想情况下,图像首先将长边缩放至 100(纵横比),然后用白色填充较小的边缘。

 .---------.
 |- - - - -|
 |  IMAGE  |
 |- - - - -|
 '---------'

我不使用 Paperclip 或 Rails,只使用 RMagick。

I want to resize/scale an image. The originals have not the same dimensions like 300x200 or 512x600. I want to resize the image to 100x100 but DONT crop anything from the image or change ratio. Ideally the image will be first scale the long edge to 100 (aspect ratio) and then fill up the smaller edge with white.

 .---------.
 |- - - - -|
 |  IMAGE  |
 |- - - - -|
 '---------'

I dont use Paperclip or Rails, just RMagick.

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

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

发布评论

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

评论(4

世界等同你 2024-10-18 16:59:56

我通过将调整大小的图像与新的 100x100 图像合并来完成此操作。这肯定不是最好的方法,但它有效:

img = Magick::Image.read("file.png").first
target = Magick::Image.new(100, 100) do
  self.background_color = 'white'
end
img.resize_to_fit!(100, 100)
target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write("file-small.png)

I've done it with merging the resized image with a new 100x100 image. That's for sure not the best way but it works:

img = Magick::Image.read("file.png").first
target = Magick::Image.new(100, 100) do
  self.background_color = 'white'
end
img.resize_to_fit!(100, 100)
target.composite(img, Magick::CenterGravity, Magick::CopyCompositeOp).write("file-small.png)
翻了热茶 2024-10-18 16:59:56

玩了一段时间后,我得到了 Fu86 的复合技巧,如下所示:

img = Image.read("some_file").first().resize_to_fit!(width, height)
target = Image.new(width, height) do
    self.background_color = 'white'
end
target.composite(img, CenterGravity, AtopCompositeOp).write("some_new_file")

AtopCompositeOp 似乎比 CopyCompositeOp 效果更好,后者由于某种原因将我的部分背景变成了黑色。

After playing with it for a while I got Fu86's composite trick to work like so:

img = Image.read("some_file").first().resize_to_fit!(width, height)
target = Image.new(width, height) do
    self.background_color = 'white'
end
target.composite(img, CenterGravity, AtopCompositeOp).write("some_new_file")

AtopCompositeOp seems to work better than CopyCompositeOp, which turned part of my background black for some reason.

初心 2024-10-18 16:59:56
image = Magick::Image.read("filename").first
resized = image.resize_to_fit(width, height)     # will maintain aspect ratio, so one of the resized dimensions may be less than the specified dimensions
resized.background_color = "#FFFFFF"             # without a default, background color will vary based on the border of your original image
x = (resized.columns - width) / 2                # calculate necessary translation to center image on background
y = (resized.rows - height) / 2
resized = resized.extent(width, height, x, y)    # 'extent' fills out the resized image if necessary, with the background color, to match the full requested dimensions. the x and y parameters calculated in the previous step center the image on the background.
resized.write("new_filename")

注意:在heroku上,截至本文发布时,它使用imagemagick 6.5.7-8,我需要将x和y翻译乘以-1(并发送正数)。版本 6.8.0-10 需要负数。

image = Magick::Image.read("filename").first
resized = image.resize_to_fit(width, height)     # will maintain aspect ratio, so one of the resized dimensions may be less than the specified dimensions
resized.background_color = "#FFFFFF"             # without a default, background color will vary based on the border of your original image
x = (resized.columns - width) / 2                # calculate necessary translation to center image on background
y = (resized.rows - height) / 2
resized = resized.extent(width, height, x, y)    # 'extent' fills out the resized image if necessary, with the background color, to match the full requested dimensions. the x and y parameters calculated in the previous step center the image on the background.
resized.write("new_filename")

Note: on heroku, which as of this posting uses imagemagick 6.5.7-8, I needed to multiply the x and y translations by -1 (and send positive numbers). Version 6.8.0-10 expects negative numbers.

满身野味 2024-10-18 16:59:56

看来你想使用 change_geometry...

It seems you want to use change_geometry...

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