Skip to content

Instantly share code, notes, and snippets.

@ashleymason
Created December 7, 2018 18:15
Show Gist options
  • Save ashleymason/264f0555f1d5d1d343d571d02ae3037c to your computer and use it in GitHub Desktop.
Save ashleymason/264f0555f1d5d1d343d571d02ae3037c to your computer and use it in GitHub Desktop.
RMagick (ImageMagick) demo
# What is Imagemagick?
# open-source software for altering image files
# developed in 1987 by John Cristy for DuPont (older than Ruby!)
# originally developed to convert 24-bit images (16 million color) to
# 8-bit images (256-color), so they could be displayed on most screens
# copyright released in 1990 to ImageMagick Studio LLC, which is the organization
# that still maintains it today.
# https://github.com/rmagick/rmagick
# https://www.imagemagick.org/script/index.php
require 'RMagick'
include Magick
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
# resize with Imagemagick:
# System.execute('convert cat.jpg -resize 100x100 resized_cat.jpg')
# with RMagick:
cat.resize(100, 100).write('heavy_breathing_cat_thumb.jpg')
# rotated 180 degrees
rotated_cat = cat.rotate!(180).write('heavy_breathing_cat_rotated.jpg')
# flop
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
cat.flop!.write('heavy_breathing_cat_flopped.jpg')
# invert
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
cat.level_colors(black_color="white", white_color="black", invert=true).
write('heavy_breathing_cat_inverted.jpg')
# oil painting
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
cat.oil_paint(radius=3.0).write('oil_paint.jpg')
# frame
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
cat.frame(width=25, height=25, x=25, y=25, inner_bevel=6, outer_bevel=6).
write('heavy_breathing_cat_with_frame.jpg')
# nightmare fuel
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
cat.implode(amount=0.50).write('implode.jpg')
# flip it and reverse it
Image.new(343, 686).write('empty.jpg')
blank_file = Magick::Image.read('empty.jpg').first
cat = Magick::Image.read('heavy_breathing_cat.jpg').first
blank_file.composite(cat, NorthWestGravity, Magick::OverCompositeOp).
write('with_top_cat.jpg')
Magick::Image.read('with_top_cat.jpg').first.
composite(rotated_cat, SouthWestGravity, Magick::OverCompositeOp).
write('with_bottom_cat.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment