File: transparent.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 (37 lines) | stat: -rw-r--r-- 1,102 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
31
32
33
34
35
36
37
require 'rmagick'

# Demonstrate the Image#transparent method.
# Change all black pixels in the image to transparent.

before = Magick::Image.new(200, 200) do |options|
  options.background_color = 'black'
end

circle = Magick::Draw.new
circle.fill('transparent')
circle.stroke('white')
circle.stroke_width(8)
circle.circle(100, 100, 180, 100)
circle.fill('transparent')
circle.stroke('white')
circle.circle(60, 100, 40, 100)
circle.circle(140, 100, 120, 100)
circle.circle(100, 60, 100, 40)
circle.circle(100, 140, 100, 120)
circle.draw(before)

before.compression = Magick::LZWCompression
before.write('transparent_before.gif')

before.fuzz = 100
after = before.transparent('black', alpha: Magick::TransparentAlpha)

# Different way of reading an image - start with an imagelist.
# Use the plasma image as a background so we can see that
# the black pixels have been made transparent.
bg = Magick::ImageList.new
bg.read('plasma:purple-gold') { |options| options.size = '200x200' }

after = bg.composite(after, Magick::CenterGravity, Magick::OverCompositeOp)
after.write('transparent_after.gif')
exit