class ImageParadise::ImageToAscii

Constants

BR
#

BR

#
HTML
#

HTML

#

Attributes

image_chars[W]

Public Class Methods

[](i) click to toggle source
#

ImageToAscii[]

Usage example:

require 'image_to_ascii'
ImageToAscii['screenshot_2014.png']
#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 62
def self.[](i)
  _ = ImageToAscii.new(i)
  _.to_ascii
end
new( path_to_file, run_already = true ) click to toggle source
#

initialize

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 31
def initialize(
    path_to_file,
    run_already = true
  )
  reset
  # open-uri open will fallback to IO open.
  path_to_file = [path_to_file] if path_to_file.is_a? String
  if path_to_file
    path_to_file.each {|path|
      open(path) { |file| @data = file.read }
    }
  end
  self
end

Public Instance Methods

image_chars() click to toggle source
#

image_chars

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 142
def image_chars
  @image_chars ||= ' .~:+=o*x^%#@$MW'.chars.to_a
end
inspect() click to toggle source
#

inspect

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 149
def inspect
  "#<#{self.class.to_s}>"
end
reset() click to toggle source
#

reset

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 70
def reset
  @data = nil
end
result()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 49
def result?
  @result
end
Also aliased as: result
to_ascii(options = {}) click to toggle source
#

to_ascii

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 77
def to_ascii(options = {})
  options = {
    width: 100, color: false, format: 'text'
  }.merge(options)

  img = Magick::Image.from_blob(@data).first

  width  = options[:width]
  scale  = (width.to_f / img.columns)
  height = ((img.rows * scale) / 2).to_i

  img.resize!(width, height)
  # ======================================================================= #
  # Check whether we wish to add colours.
  # ======================================================================= #
  color_image   = img.dup if options[:color]
  img           = img.quantize(
                    image_chars.length, Magick::GRAYColorspace
                  ).normalize
  quantum_calc  = Magick::QuantumRange / Magick::QuantumPixel.to_i
  image_chars.map! {|char|
    char == ' ' ? '&nbsp;' : char
  } if options[:format] == HTML

  border = "+#{'-' * width}+#{line_break(options[:format])}"
  border = html_char(border) if options[:format] == HTML

  output = border.dup

  img.view(0, 0, width, height) { |view|
    height.times { |i|
      output << '|'
      width.times { |j|
        character = image_chars[view[i][j].red/quantum_calc]
        if options[:format] == HTML
          if options[:color]
            pix = color_image.pixel_color(j,i)
            color_string = "color: #{pix.to_color( Magick::AllCompliance,false,8, true)};"
          else
            color_string = ''
          end
          character = html_char(character, color_string)
        else
          # text-format
          if options[:color]
            pix       = color_image.pixel_color(j,i)
            character = character.color(
              unified_rgb_value(pix.red),
              unified_rgb_value(pix.green),
              unified_rgb_value(pix.blue)
            )
          end
        end
        output << character
      }
      output << "|#{line_break(options[:format])}"
    }
  }
  @result = output + border
  return @result
end

Private Instance Methods

html_char(char, additional_style = '') click to toggle source
#

html_char

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 170
def html_char(char, additional_style = '')
  "<span style=\"font-family: 'Lucida Console', Monaco, monospace; #{additional_style}\">#{char}</span>"
end
line_break(format) click to toggle source
#

line_break

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 156
def line_break(format)
  (format == 'text') ? "\n" : BR
end
unified_rgb_value(number) click to toggle source
#

unified_rgb_value

#
# File lib/image_paradise/image_to_ascii/image_to_ascii.rb, line 163
def unified_rgb_value(number)
  (Magick::QuantumDepth == 16) ? (number / 256) : number
end