Sha256: af1fb04c29a35b92eca540fa558526044c612579b17ab05ac2412009d4e69e56

Contents?: true

Size: 1.54 KB

Versions: 13

Compression:

Stored size: 1.54 KB

Contents

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

# Demonstrate the Image#stegano method.

# Create a small watermark from the Snake image by
# shrinking it and converting it to B&W.
begin
  watermark = Magick::Image.read('images/Snake.wmf').first
  watermark.scale!(64.0/watermark.rows)
  watermark = watermark.quantize(256, Magick::GRAYColorspace)
  wmrows = watermark.rows
  wmcols = watermark.columns

  # Read the image in which we'll hide the watermark.
  img = Magick::Image.read('images/Flower_Hat.jpg').first
  img.scale!(250.0/img.rows)

  # Embed the watermark starting at offset 91.
  puts 'Embedding watermark...'
  stegano = img.stegano(watermark, 91)
  puts 'Watermark embedded'

  # Write the watermarked image in MIFF format. Note that
  # the format must be lossless - Image#stegano doesn't work
  # with lossy formats like JPEG.
  stegano.write('img.miff')

  # Read the image and retrieve the watermark. The size
  # attribute describes the size and offset of the watermark.

  # This can take some time. Keep track of how far along we are.

  monitor = proc do |text, quantum, span|
    printf("Extracting watermark...%3.0f%% complete\n", ((1.0-(quantum/span.to_f))*100.0))
    true
  end

  stegano = Magick::Image.read('stegano:img.miff') do
    self.size = Magick::Geometry.new(wmcols, wmrows, 91)
    self.monitor = monitor
  end

  stegano[0].monitor = nil

  # We don't need this any more.
  File.delete('img.miff')

  stegano[0].write('stegano.gif')

rescue Magick::ImageMagickError
  puts "#{$PROGRAM_NAME}: ImageMagickError - #{$ERROR_INFO}"
end

exit

Version data entries

13 entries across 13 versions & 3 rubygems

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