[Awesome Ruby Gem] Use imagemagick library and rmagick gem to process images in Ruby

rmagick

RMagick is an interface between the Ruby programming language and the ImageMagick image processing library.

Prerequisites

These prerequisites are required for the latest version of RMagick.

On Ubuntu, you can run:

1
$ sudo apt-get install libmagickwand-dev

On Centos, you can run:

1
$ sudo yum install gcc ImageMagick-devel make which

On Arch Linux, you can run:

1
$ pacman -Syy pkg-config imagemagick

On Alpine Linux, you can run:

1
$ apk add pkgconfig imagemagick imagemagick-dev imagemagick-libs

On macOS, you can run:

1
$ brew install pkg-config imagemagick

Installation

You can install it as a gem:

1
$ gem install rmagick

or add it into a Gemfile (Bundler):

1
2
3
4
5
# Gemfile

# rmagick/rmagick: Ruby bindings for ImageMagick
# https://github.com/rmagick/rmagick
gem 'rmagick', '4.2.2'

Then, run bundle install.

1
$ bundle install

Usages

Basic concepts

Let’s look at the RMagick equivalent of “Hello, world”. This program reads an image file named “Cheetah.jpg” and display it on your monitor.

1
2
3
4
5
require 'RMagick'
include Magick

cat = ImageList.new("Cheetah.jpg")
cat.display

Reading, writing, and creating images

For example, you can set the background color of a new image to red with the background_color= method, as shown here:

1
2
3
4
5
require 'RMagick'
include Magick
# Create a 100x100 red image.
f = Image.new(100,100) { self.background_color = "red" }
f.display

For example, the following program reads three GIF files and then uses ImageList#write to combine all the images in those files (remember, each input file can contain multiple images) into one animated GIF file.

1
2
3
4
#! /usr/local/bin/ruby -w
require 'RMagick'
anim = ImageList.new("start.gif", "middle.gif", "finish.gif")
anim.write("animated.gif")

For example, the following program, read a pdf file and convert to JPG file.

1
2
3
require 'RMagick'
pdf = Magick::ImageList.new("doc.pdf")
pdf.write("myimage.jpg")

As an example, here’s the section of the Ruby program that created the circle in the center of the above image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
!# /usr/local/bin/ruby -w
require 'RMagick'

canvas = Magick::ImageList.new
canvas.new_image(250, 250, Magick::HatchFill.new('white', 'gray90'))

circle = Magick::Draw.new
circle.stroke('tomato')
circle.fill_opacity(0)
circle.stroke_opacity(0.75)
circle.stroke_width(6)
circle.stroke_linecap('round')
circle.stroke_linejoin('round')
circle.ellipse(canvas.rows/2,canvas.columns/2, 80, 80, 0, 315)
circle.polyline(180,70, 173,78, 190,78, 191,62)
circle.draw(canvas)

Annotation

The annotate method draws text on an image. In its simplest form, annotate requires only arguments that describe where to draw the text and the text string.

The following example shows how to use annotate to produce this image.

annotate example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/local/bin/ruby -w
require 'RMagick'

# Demonstrate the annotate method

Text = 'RMagick'

granite = Magick::ImageList.new('granite:')
canvas = Magick::ImageList.new
canvas.new_image(300, 100, Magick::TextureFill.new(granite))

text = Magick::Draw.new
text.font_family = 'helvetica'
text.pointsize = 52
text.gravity = Magick::CenterGravity

text.annotate(canvas, 0,0,2,2, Text) {
self.fill = 'gray83'
}

text.annotate(canvas, 0,0,-1.5,-1.5, Text) {
self.fill = 'gray40'
}

text.annotate(canvas, 0,0,0,0, Text) {
self.fill = 'darkred'
}

canvas.write('rubyname.gif')

References

[1] rmagick/rmagick: Ruby bindings for ImageMagick - https://github.com/rmagick/rmagick

[2] RMagick 2.12.0: How to use RMagick - https://rmagick.github.io/usage.html

[3] rmagick | RubyGems.org | your community gem host - https://rubygems.org/gems/rmagick

[4] ImageMagick - Image Formats - http://www.imagemagick.org/script/formats.php

[5] imagemagick — Homebrew Formulae - https://formulae.brew.sh/formula/imagemagick

[6] pkg-config — Homebrew Formulae - https://formulae.brew.sh/formula/pkg-config