Sha256: 2103d0797b70935c26526e60281381c1f47e013d4ae9d7315dea696bb3960d59

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

#!/usr/bin/env ruby -w

#=======================================================#
# Thanks to Robert Wagner for the idea of allowing a    #
# GravityType instead of the x- and y-offset arguments! #
#=======================================================#

# Demo the use of the GravityType argument to Image#crop.

require 'rmagick'
include Magick

shorts = Image.read('../doc/ex/images/Shorts.jpg').first

regwidth = shorts.columns / 2
regheight = shorts.rows / 2

mask = Image.new(regwidth, regheight) { self.background_color = 'white' }
mask.alpha(Magick::ActivateAlphaChannel)
mask.quantum_operator(SetQuantumOperator, 0.50 * QuantumRange, AlphaChannel)

black = Image.new(shorts.columns, shorts.rows) { self.background_color = 'black' }
pairs = ImageList.new

[NorthWestGravity, NorthGravity, NorthEastGravity,
 WestGravity, CenterGravity, EastGravity,
 SouthWestGravity, SouthGravity, SouthEastGravity].each do |gravity|
  pattern = shorts.composite(mask, gravity, OverCompositeOp)
  cropped = shorts.crop(gravity, regwidth, regheight)
  result = black.composite(cropped, gravity, OverCompositeOp)
  result.border_color = 'white'
  pairs << pattern
  pairs << result
end

# Montage into a single image
montage = pairs.montage do
  self.geometry = "#{pairs.columns}x#{pairs.rows}+0+0"
  self.tile = '6x3'
  self.border_width = 1
end
montage.write('crop_with_gravity.png')
# montage.display

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rmagick-4.1.0.rc2 examples/crop_with_gravity.rb
rmagick-4.1.0.rc1 examples/crop_with_gravity.rb
rmagick-4.0.0 examples/crop_with_gravity.rb