Sha256: 66c7aa0826084a3eea26893b7f34fd8da8fd3c8f654e930e94af803dd0267bd9

Contents?: true

Size: 1.62 KB

Versions: 13

Compression:

Stored size: 1.62 KB

Contents

#!/usr/bin/env ruby -w
require 'rmagick'

# Demonstrate partial transparency and the get_pixels and
# store_pixels methods by creating an image that goes from
# full-color on the left to monochrome on the right.

# Read the colorful picture of a rock formation. Scale
# it to 300 pixels high because we don't want a big picture.
rocks = Magick::Image.read('images/Red_Rocks.jpg').first
rocks.scale!(250.0/rocks.rows)

# Make a monochrome copy. See Image#quantize for details
grayrocks = rocks.quantize(256, Magick::GRAYColorspace)

rows = grayrocks.rows
cols = grayrocks.columns

# Create an array of opacity values, proceeding from
# transparent to opaque. The array should have as many
# elements as there are columns in the image. The first
# element should be TransparentOpacity and each succeeding
# element slightly more opaque than its predecessor.
step = Magick::TransparentOpacity / cols.to_f
opacity_steps = Array.new(cols)
cols.times do |x|
  opacity_steps[x] = Magick::TransparentOpacity - Integer(x * step)
  if opacity_steps[x] < Magick::OpaqueOpacity
    opacity_steps[x] = Magick::OpaqueOpacity
  end
end

# Get each row of pixels from the mono image.
# Copy the pre-computed opacity values to the pixels.
# Store the pixels back.
rows.times do |y|
  pixels = grayrocks.get_pixels(0, y, cols, 1)
  pixels.each_with_index { |p,x| p.opacity = opacity_steps[x] }
  grayrocks.store_pixels(0, y, cols, 1, pixels)
end

# Composite the mono version of the image over the color version.
grayrocks.matte = true
combine = rocks.composite(grayrocks, Magick::CenterGravity, Magick::OverCompositeOp)
#combine.display
combine.write 'get_pixels.jpg'
exit

Version data entries

13 entries across 13 versions & 3 rubygems

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