ruby Rmagick : Crop/"Trim" excessive (white) space of image automatically

标签 ruby imagemagick rmagick imgkit

我正在使用 IMGkit ( https://github.com/csquared/IMGKit ) 拍摄 HTML 文档的“屏幕截图”。 背景始终为白色 (#ffffff)。

IMGkit 正在拍摄 1200 x 1000 像素的大“屏幕截图”,我最终可能会看到很多空白区域,因为 HTML 文档的实际内容可能非常小。

Rmagick 中是否有内置函数可以自动裁剪/修剪过多的空白区域?

最佳答案

解决方法是RMagick的trim方法。

例子

require "rubygems"
require "RMagick"
require "imgkit"

include Magick

kit  = IMGKit.new('http://localhost/test.html')
blob = kit.to_img(:png)

img = Magick::Image.from_blob(blob).first
img.trim!

File.open("test.png", "w") { |f| f << img.to_blob }

关于 ruby Rmagick : Crop/"Trim" excessive (white) space of image automatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15613382/

相关文章:

ruby - Sinatra,在哪里放置 require 语句

ruby - 是否可以从一对多关系的 belongs_to 端使用 mongoid "nested attributes"?

ruby-on-rails - 启用 rails 的 Rubocop 和 Style/IndentationConsistency

python - 说服其他人使用 Ruby 而不是 Python 和 PHP

ruby-on-rails - libMagickCore.so.4 : cannot open shared object file: No such file or directory

c - 如何在 ImageMagick Core 中从图像中提取图像信息

imagemagick - 使用Sphinx Extension将svg转换为pdf

linux - 在行命令中调整图像列表的大小

ruby - rmagick 压缩并将 png 转换为 jpg

ruby-on-rails - Windows 7 上的 Ruby on Rails RMagick