Simplest correct ImageMagick/GraphicsMagick ruby wrapper
MicroMagick lets you use ImageMagick or GraphicsMagick from ruby.
Using MicroMagick:
rmagick
)640x480>
(which doesn't work
with mini_magick
and quick_magick
)mini_magick
)imagery
)MicroMagick has excellent test coverage and code quality.
img = MicroMagick::Image.new("/path/to/image.jpg")
img.strip.quality(85).resize("640x480>").write("640x480.jpg") unless img.corrupt?
This will
Read /path/to/image.jpg
Verify the image is not corrupt, using identify
Set up a convert
command to
strip
),This results in the following system call:
gm convert -size 640x480 /path/to/image.jpg +profile \* -quality 85 -resize "640x480>" /new/path/image-640x480.jpg
To get image dimensions:
img.width
# => 3264
img.height
# => 2448
To access EXIF metadata information, including properly typed values, see the exiftool and exiftool_vendored gems.
Add gem 'micro_magick'
to your Gemfile
and run bundle
.
You'll also need to install GraphicsMagick.
If you're on a Mac with homebrew, brew install graphicsmagick
.
To add an output option that has a "plus" prefix, like, +matte
, use
.add_output_option("+matte")
.
There are a couple additional methods that have been added to address common image tasks:
img.strip
removes all comments and EXIF headersimg.square_crop
crops the image to a square (so a 640x480 image would crop
down to a 480x480 image, cropped in the middle).Note that micro_magick
delegates all parameter validation to the underlying
library. A MicroMagick::ArgumentError
will be raised on .write
or
.overwrite
if ImageMagick or GraphicsMagick writes anything to stderr.
At least in my testing, GraphicsMagick blows ImageMagick out of the water.
In resizing a 2248x4000 image to 640x480:
Not only is GraphicsMagick 4 times faster, it produces 2.5x smaller output with the same quality--WIN WIN.
Because of this, if you have GraphicsMagick installed, MicroMagick will use it
by default. You can force which library to use with the
MicroMagick.use_graphicsmagick!
and MicroMagick.use_imagemagick!
methods.
To see which external library and version you're using:
MicroMagick.version
=> #<Gem::Version "1.3.21">
MicroMagick.imagemagick?
=> false
MicroMagick.graphicsmagick?
=> true
Added explicit MIT licensing.
Please note that the attributes hash associated to images has been removed in this version, in the interests of correctness. If you need EXIF metadata, use the exiftool gem.
identify -verbose
parsing.overwrite
methodCleaned up some packaging/mode bit issues
Fixed gemspec metadata
Input file -size render hint is now only used with simple dimension specifications
Added square_crop, image dimensions, and support for +option
s.
Let's get this party started.