Created
April 24, 2012 18:24
-
-
Save aitor/2482336 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'digest/md5' | |
require 'sinatra' | |
require 'RMagick' | |
include Magick | |
get '/avatar' do | |
format = 'png' | |
content_type format | |
w,h = 300,300 | |
face = Image.new(w,h) | |
face.format = format | |
#face.draw img | |
eyes(face) | |
mouth(face) | |
face = face.gaussian_blur(0.0, 4.0) | |
face = face.blend perlin, 0.85, 0.974 | |
#face = face.black_threshold 65535 | |
face = face.white_threshold 65535 | |
face.to_blob | |
end | |
def perlin | |
Image.read(File.expand_path(File.dirname(__FILE__) +'/perlin.png'))[0] | |
end | |
def eyes(face) | |
center = face.columns/2 | |
eye_distance = face.columns/6 | |
gaze_line = face.rows/6 | |
radius = face.columns/10 | |
eyes = Draw.new | |
leyex = center - eye_distance | |
reyex = center + eye_distance | |
#add_round_eye eyes, leyex, gaze_line, radius | |
#add_round_eye eyes, reyex, gaze_line, radius | |
add_square_eye eyes, leyex, gaze_line, radius, radius*3 | |
add_square_eye eyes, reyex, gaze_line, radius, radius*3 | |
eyes.draw face | |
end | |
def add_round_eye(eyes, x, y, r) | |
eyes.circle x, y, | |
x+r, y | |
end | |
def add_square_eye(eyes,x,y,w,h) | |
eyes.rectangle x-w, y-w, x+w, y+h | |
end | |
def mouth(face) | |
mouth = Draw.new | |
center = face.columns/2 | |
mouth_line = face.rows - face.rows/2.3 | |
mouth_curvature = mouth_line + face.rows/1.8 | |
mouth_width = face.columns/4 | |
mouth.bezier center-mouth_width,mouth_line, | |
center-mouth_width,mouth_curvature, | |
center+mouth_width,mouth_curvature, | |
center+mouth_width,mouth_line | |
mouth.draw face | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment