Skip to content

Instantly share code, notes, and snippets.

@rkkautsar
Created March 13, 2016 01:29
Show Gist options
  • Save rkkautsar/e0773528faaf64eef8c4 to your computer and use it in GitHub Desktop.
Save rkkautsar/e0773528faaf64eef8c4 to your computer and use it in GitHub Desktop.
Steganography (RMagick)
#! /usr/bin/env ruby
require 'rmagick'
if ARGV.length < 3
puts "Usage: #{$0} input_file data output_file"
abort("ERROR: Wrong arguments")
end
input = ARGV[0]
data = ARGV[1]
output = ARGV[2]
image = Magick::Image.read(input).first
data = IO.binread(data).unpack("B*")[0].split("").map(&:to_i)
col = image.columns
row = image.rows
if data.length > col * row
abort("Data is too big for the image to contains.")
end
idx = 0
len = data.length
pixels = image.get_pixels(0, 0, col, row)
for pixel in pixels
# clear lsb
pixel.red &= ~1
pixel.green &= ~1
pixel.blue &= ~1
if idx < data.length
pixel.red |= data[idx]
pixel.green |= data[idx]
pixel.blue |= data[idx]
idx += 1
end
end
out = Magick::Image.new(col, row)
out.store_pixels(0, 0, col, row, pixels)
out.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment