File: crop_with_gravity.rb

package info (click to toggle)
ruby-rmagick 2.13.2-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,620 kB
  • ctags: 1,785
  • sloc: ansic: 16,759; ruby: 9,717; makefile: 14; sh: 14
file content (46 lines) | stat: -rw-r--r-- 1,368 bytes parent folder | download
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env ruby

#=======================================================#
# 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(File.join(File.dirname(__FILE__), '../doc/ex/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 {
    self.geometry = "#{pairs.columns}x#{pairs.rows}+0+0"
    self.tile = "6x3"
    self.border_width = 1
    }
montage.write('crop_with_gravity.miff')
#montage.display