Class: ASCII_Image
- Inherits:
-
Object
- Object
- ASCII_Image
- Defined in:
- lib/ascii-image.rb
Overview
Summary
This handy Ruby gem will help you to create awesome ASCII art from images for your awesome command-line projects.
Example
ascii = ASCII_Image.new("file.jpg")
ascii.build(80)
Contact
- Author
-
Nathan Campos ([email protected])
- Website
Instance Method Summary collapse
-
#build(width) ⇒ Object
Convert the image into ASCII and print it to the console.
-
#initialize(file, console_width = 80) ⇒ ASCII_Image
constructor
Initialize the ASCII_Image class.
Constructor Details
#initialize(file, console_width = 80) ⇒ ASCII_Image
Initialize the ASCII_Image class.
An Error is raised if your ImageMagick quantum depth is higher than 8.
Arguments:
file: (String)
console_width: (Integer)
31 32 33 34 35 36 37 38 |
# File 'lib/ascii-image.rb', line 31 def initialize(file, console_width = 80) @file = file @console_width = console_width if Magick::QuantumDepth > 8 raise "Your ImageMagick quantum depth is set to #{Magick::QuantumDepth}. You need to have it set to 8 in order for this app to work." end end |
Instance Method Details
#build(width) ⇒ Object
Convert the image into ASCII and print it to the console.
An ArgumentError is raised if the width
is bigger than the console_width
Arguments:
width: (Integer)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ascii-image.rb', line 47 def build(width) # Open the image file image = Magick::Image.read(@file)[0] # Resize to the desired "text-pixel" size if width > @console_width raise ArgumentError, "The desired width is bigger than the console width" end image = image.scale(width / image.columns.to_f) # Compensate the height of the console "text-pixel" image = image.scale(image.columns, image.rows / 1.7) # Get the pixel array image.each_pixel do |pixel, col, row| print " ".background(pixel.red, pixel.green, pixel.blue) if (col % (width - 1) == 0) and (col != 0) print "\n" end end print "\n" end |