class Ascmaster::ImgToAsciiConverter
Constants
- ASCII_CHARS
Public Class Methods
convert(image_name, width = 70)
click to toggle source
# File lib/ascmaster/img_to_ascii_converter.rb, line 35 def convert(image_name, width = 70) image = ImageList.new(image_name) scaled_image = scale(image, width) grayscale = make_grayscale(scaled_image) ascii = image_to_ascii(grayscale) print_ascii(ascii, width) end
image_to_ascii(image)
click to toggle source
# File lib/ascmaster/img_to_ascii_converter.rb, line 10 def image_to_ascii(image) ascii = [] image.each_pixel do |pixel| scaled_pixel = pixel.intensity.to_f / MaxRGB ascii << ASCII_CHARS[((ASCII_CHARS.length - 1) * scaled_pixel).round] end return ascii end
make_grayscale(image)
click to toggle source
# File lib/ascmaster/img_to_ascii_converter.rb, line 19 def make_grayscale(image) image.quantize(256, Magick::GRAYColorspace) end
print_ascii(ascii, width)
click to toggle source
# File lib/ascmaster/img_to_ascii_converter.rb, line 28 def print_ascii(ascii, width) ascii.each_with_index do |char, index| print char print "\n" if index % width == 0 end end
scale(image, width)
click to toggle source
# File lib/ascmaster/img_to_ascii_converter.rb, line 23 def scale(image, width) image = image.scale(width / image.columns.to_f) image.scale(image.columns, image.rows / 2.3) end