Class: ASCII_Image

Inherits:
Object
  • Object
show all
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

about.me/nathanpc

Instance Method Summary collapse

Constructor Details

#initialize(uri, console_width = 80) ⇒ ASCII_Image

Initialize the ASCII_Image class.

An Error is raised if your ImageMagick quantum depth is higher than 8.

Arguments:

uri: (String)
console_width: (Integer)


32
33
34
35
36
37
38
39
# File 'lib/ascii-image.rb', line 32

def initialize(uri, console_width = 80)
    @uri = uri
    @console_width = console_width
    
    if Magick::MAGICKCORE_QUANTUM_DEPTH > 8
        raise "Your ImageMagick quantum depth is set to #{Magick::MAGICKCORE_QUANTUM_DEPTH}. 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)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ascii-image.rb', line 48

def build(width)
    # Open the resource
    resource = open(@uri)
    image = Magick::ImageList.new
    image.from_blob resource.read
    
    # 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 Rainbow(" ").background(pixel.red, pixel.green, pixel.blue)
        
        if (col % (width - 1) == 0) and (col != 0)
            print "\n"
        end
    end
end