File: crop.rb

package info (click to toggle)
ruby-rmagick 6.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,232 kB
  • sloc: cpp: 19,563; ruby: 17,147; sh: 88; javascript: 36; makefile: 13
file content (30 lines) | stat: -rw-r--r-- 780 bytes parent folder | download | duplicates (2)
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
require 'rmagick'

# Demonstrate the Image#crop method

img = Magick::Image.read('images/Flower_Hat.jpg')[0]

# Crop the specified rectangle out of the img.
chopped = img.crop(23, 81, 107, 139)

# Go back to the original and highlight the area
# corresponding to the retained rectangle.
rect = Magick::Draw.new
rect.stroke('transparent')
rect.fill('white')
rect.fill_opacity(0.25)
rect.rectangle(23, 81, 107 + 23, 139 + 81)
rect.draw(img)

img.write('crop_before.png')

# Create a image to use as a background for
# the "after" image.
bg = Magick::Image.new(img.columns, img.rows) { |info| info.background_color = 'none' }

# Composite the the "after" (chopped) image on the background
bg = bg.composite(chopped, 23, 81, Magick::OverCompositeOp)

bg.write('crop_after.png')

exit