Class: Artascii::Image
- Inherits:
-
Object
- Object
- Artascii::Image
- Defined in:
- lib/artascii.rb
Constant Summary collapse
- MAX_BRIGHTNESS =
255
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #get_ascii_mx(matrix) ⇒ Object
- #get_brightness_mx(matrix) ⇒ Object
- #get_pixels ⇒ Object
-
#initialize(path, resize_percent = 100) ⇒ Image
constructor
A new instance of Image.
- #modify_ascii_mx ⇒ Object
- #print_to_file(matrix, path = nil) ⇒ Object
- #to_char(brightness) ⇒ Object
Constructor Details
#initialize(path, resize_percent = 100) ⇒ Image
Returns a new instance of Image.
12 13 14 15 16 17 18 |
# File 'lib/artascii.rb', line 12 def initialize(path, resize_percent=100) @image = MiniMagick::Image.open(path) @height = @image.height @width = @image.width @matrix = @image.resize resize_percent self end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
8 9 10 |
# File 'lib/artascii.rb', line 8 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
8 9 10 |
# File 'lib/artascii.rb', line 8 def width @width end |
Instance Method Details
#get_ascii_mx(matrix) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/artascii.rb', line 34 def get_ascii_mx(matrix) ascii_mx = Array.new matrix.each do |line| line_mx = Array.new line.each { |pixel| line_mx.append(to_char(pixel)) } ascii_mx.append(line_mx) end # print_mx(ascii_mx) ascii_mx end |
#get_brightness_mx(matrix) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/artascii.rb', line 24 def get_brightness_mx(matrix) bright_mx = Array.new matrix.each do |line| line_mx = Array.new line.each { |pixel| line_mx.append(pixel.sum/pixel.length) } bright_mx.append(line_mx) end bright_mx end |
#get_pixels ⇒ Object
20 21 22 |
# File 'lib/artascii.rb', line 20 def get_pixels @image.get_pixels end |
#modify_ascii_mx ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/artascii.rb', line 45 def modify_ascii_mx ascii_mx = Array.new @matrix.each do |line| line_mx = Array.new line.each { |pixel| yield } ascii_mx.append(line_mx) end ascii_mx end |
#print_to_file(matrix, path = nil) ⇒ Object
55 56 57 58 59 |
# File 'lib/artascii.rb', line 55 def print_to_file(matrix, path=nil) File.open(path, "w") do |f| matrix.each { |line| f.puts line.join() + '\n'} end end |
#to_char(brightness) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/artascii.rb', line 61 def to_char(brightness) char_s = "`^\",:;Il!i~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$".freeze char_arr = char_s.split("") index = (brightness * char_arr.length)/MAX_BRIGHTNESS - 1 char_arr[index] * 3 end |