Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created January 28, 2012 11:40
Show Gist options
  • Save b1nary/1694036 to your computer and use it in GitHub Desktop.
Save b1nary/1694036 to your computer and use it in GitHub Desktop.
convert text to an binary based Image.

rBinmage

Converts text to Images based on 1 and 0. They can easily get coded and decoded. Works best with gif or Bitmap images.

Needs: rMagick

Example, the Script itself Encrypted:

Binary Image Example

It would be easy to implement some secure crypting. just take a look at this: https://gist.github.com/1666901

#/usr/bin/env ruby
require 'optparse'
require 'RMagick'
include Magick
MINCOLOR="rgb(255,255,255)"
MAXCOLOR="rgb(0,0,0)"
options = {}
optparse = OptionParser.new do|opts|
opts.banner = "Usage: ruby hidemage.rb [options] IMG-FILE.png"
options[:read] = false
opts.on( '-r', '--read FILE', "Read from exisiting Image" ) do |file|
options[:read] = file
end
options[:write] = false
opts.on( '-w', '--write STRING|FILE', "Crypt String or Filecontent\n\n" ) do |string|
options[:write] = string
end
options[:out] = "out.gif"
opts.on( '-o', '--output FILE', 'Specify outputfile (default: out.gif)' ) do |string|
options[:out] = string
end
options[:width] = 4
opts.on( '-W', '--width BYTE', 'Image width [* 8(Bits)]' ) do |string|
options[:width] = string.to_i
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
if options[:write] != false
string = options[:write]
string = File.open(options[:write]).read if File.exist?(options[:write])
puts "ERROR: No input String or File..."; exit if ((string.nil? or string.size < 4 or string == "") and options[:write] != false)
puts "Try: ruby binaryimg.rb -h\n"; exit if !options[:read] and !options[:write]
puts "INPUT: #{string}"
@chars = Array.new
string.each_byte do |b|
@chars << "%08d" % b.to_s(2)
end
out = Magick::Image.new((options[:width]*8), (@chars.size/options[:width]))
puts "Image Size: #{(options[:width]*8)}x#{@chars.size/options[:width]}"
@col = 0
@row = 0
@chars.each do |b|
b.split("").each do |n|
if n.to_s == "1"
color = MAXCOLOR
else
color = MINCOLOR
end
out.pixel_color(@col,@row,color)
if @col == (options[:width]*8)-1
@col = 0
@row += 1
else
@col += 1
end
end
end
out.write(options[:out])
end
if options[:read] != false
img = ImageList.new(options[:read]).first
out = ""
aut = Array.new
img.each_pixel do |p,c,r|
out += "1" if "rgb(#{p.red.to_i/257},#{p.green.to_i/257},#{p.green.to_i/257})" == MAXCOLOR
out += "0" if "rgb(#{p.red.to_i/257},#{p.green.to_i/257},#{p.green.to_i/257})" == MINCOLOR
if out.size == 8
aut << out.to_s
out = ""
end
end
puts "MESSAGE IN IMAGE:"
aut.each do |a|
print a.to_i(2).chr
end
puts ""
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment