class ImageParadise::ImageToAscii
Constants
- BR
#¶ ↑
BR
¶ ↑#¶ ↑
- HTML
#¶ ↑
HTML
¶ ↑#¶ ↑
Attributes
image_chars[W]
Public Class Methods
[](i)
click to toggle source
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
inspect()
click to toggle source
reset()
click to toggle source
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 == ' ' ? ' ' : 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
line_break(format)
click to toggle source