Skip to content

Instantly share code, notes, and snippets.

@siuying
Created July 22, 2009 13:52
Show Gist options
  • Save siuying/152011 to your computer and use it in GitHub Desktop.
Save siuying/152011 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'RMagick'
include Magick
PIXEL = ["&", "8", "0", "$", "2", "1", "|", ";", ":", "'", " ", " "]
colors = PIXEL.size - 1.0
# load image
oimg = ImageList.new("mario.jpg")
# resize image
img = oimg
width = (img.columns > 80) ? 80 : img.columns.to_f
height = img.rows * 0.5 * (width.to_f / img.columns.to_f)
img = img.resize(width, height)
# convert it into ascii art
view = img.view(0,0,img.columns,img.rows)
results = []
(0..img.rows-1).each do |row|
results[row] = ""
(0..img.columns-1).each do |col|
brightness = view[row][col].to_HSL[2]
results[row] += PIXEL[(brightness * colors).floor]
end
end
puts results.join("\n")
oimg.display
@wxianfeng
Copy link

k, i like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment