Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created December 10, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/67a6453d757f19ca6a55314c42a0839b to your computer and use it in GitHub Desktop.
Save jamesu/67a6453d757f19ca6a55314c42a0839b to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rmagick'
require 'digest'
def build_cat(seed=nil)
srand(Digest::MD5.hexdigest(seed.to_s)[0...6].to_i(16)) if !seed.nil?
# throw the dice for body parts
parts = [
['body', rand(15)+1],
['fur', rand(10)+1],
['eyes', rand(15)+1],
['mouth', rand(10)+1],
['accessorie', rand(20)+1]
]
# create background
cat = Magick::Image.new(70, 70) { self.background_color = 'white' }
cat.format = 'PNG'
pwd = File.expand_path(File.dirname(__FILE__))
# add parts
parts.each do |data|
part, num = data
file = "#{pwd}/avatars/#{part}_#{num}.png"
im = Magick::Image.read(file)[0]
cat.composite!(im, 0, 0, Magick::SrcOverCompositeOp)
end
# restore random seed
srand() if !seed.nil?
return cat
end
def write_cat(seed)
cat = build_cat(seed)
File.open("cat#{seed}.png", 'w') do |f|
f.write cat.to_blob
end
end
write_cat(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment