Sha256: b0a3cbe2378016d9b6d1f58783a0644cc139abf0941f69d871ac1bedefbbea71

Contents?: true

Size: 1.27 KB

Versions: 13

Compression:

Stored size: 1.27 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('images/Shorts.jpg').first

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

mask = Image.new(regwidth, regheight) { self.background_color = 'white'}
mask.opacity = 0.50 * TransparentOpacity

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.miff')
#montage.display

Version data entries

13 entries across 13 versions & 3 rubygems

Version Path
rmagick-windows-2.16.5 doc/ex/crop_with_gravity.rb
rmagick-windows-2.16.4 doc/ex/crop_with_gravity.rb
rmagick-windows-2.16.3 doc/ex/crop_with_gravity.rb
rmagick-windows-2.16.2 doc/ex/crop_with_gravity.rb
rmagick-windows-2.16.1 doc/ex/crop_with_gravity.rb
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/rmagick-2.16.0/doc/ex/crop_with_gravity.rb
rmagick-2.16.0 doc/ex/crop_with_gravity.rb
rmagick-2.15.4 doc/ex/crop_with_gravity.rb
rmagick-2.15.3 doc/ex/crop_with_gravity.rb
rmagick-2.15.2 doc/ex/crop_with_gravity.rb
rmagick-2.15.1 doc/ex/crop_with_gravity.rb
rmagick-2.15.0 doc/ex/crop_with_gravity.rb
rmagick-2.14.0 doc/ex/crop_with_gravity.rb