├── .gitignore
├── Gemfile
├── LICENSE
├── README.md
├── ascii-image.gemspec
├── examples
├── just-print-it.rb
└── poster.jpg
└── lib
└── ascii-image.rb
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | *.gem
3 | *.rbc
4 | .bundle
5 | .config
6 | .yardoc
7 | Gemfile.lock
8 | InstalledFiles
9 | _yardoc
10 | coverage
11 | doc/
12 | lib/bundler/man
13 | pkg
14 | rdoc
15 | spec/reports
16 | test/tmp
17 | test/version_tmp
18 | tmp
19 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 | gemspec
3 |
4 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | ascii-image - A Ruby gem to convert images into ASCII.
2 | Copyright (C) 2018 Nathan Campos
3 |
4 | This program is free software: you can redistribute it and/or modify
5 | it under the terms of the GNU General Public License as published by
6 | the Free Software Foundation, either version 3 of the License, or
7 | (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program. If not, see .
16 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ascii-image
2 |
3 | [](https://badge.fury.io/rb/ascii-image)
4 | A Ruby gem to convert images into ASCII for your awesome command-line applications.
5 | *This is my first Ruby gem*
6 |
7 | ## Installation
8 | This gem relies on [RMagick](https://github.com/rmagick/rmagick), so you will need to ensure you install the [prerequisites](https://github.com/rmagick/rmagick#prerequisites) (ImageMagick).
9 |
10 | Install in your terminal with:
11 | ```
12 | gem install ascii-image
13 | ```
14 |
15 | Or in your Gemfile:
16 |
17 | ```
18 | gem 'ascii-image'
19 | ```
20 |
21 | ## Example
22 |
23 | Convert an image to ASCII and print it using 20 columns of the console:
24 |
25 | ascii = ASCII_Image.new("~/my_image.jpg")
26 | ascii.build(20)
27 |
28 | or open the image from the web:
29 |
30 | ascii = ASCII_Image.new("http://www.levihackwith.com/wp-content/uploads/2011/10/github-logo.png")
31 | ascii.build(60)
32 |
33 | ## License
34 |
35 | This library is licensed under the GPLv3 license.
36 |
37 | > ascii-image - A Ruby gem to convert images into ASCII.
38 | > Copyright (C) 2012 Nathan Campos
39 | >
40 | > This program is free software: you can redistribute it and/or modify
41 | > it under the terms of the GNU General Public License as published by
42 | > the Free Software Foundation, either version 3 of the License, or
43 | > (at your option) any later version.
44 | >
45 | > This program is distributed in the hope that it will be useful,
46 | > but WITHOUT ANY WARRANTY; without even the implied warranty of
47 | > MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 | > GNU General Public License for more details.
49 | >
50 | > You should have received a copy of the GNU General Public License
51 | > along with this program. If not, see .
52 |
--------------------------------------------------------------------------------
/ascii-image.gemspec:
--------------------------------------------------------------------------------
1 | Gem::Specification.new do |s|
2 | s.name = "ascii-image"
3 | s.version = "0.1.5"
4 | s.license = "GPL-3.0"
5 | s.summary = "Convert images into ASCII"
6 | s.description = "A Ruby gem to convert images into ASCII for your awesome command-line applications"
7 | s.authors = ["Nathan Campos"]
8 | s.email = "nathan@innoveworkshop.com"
9 | s.files = ["lib/ascii-image.rb"]
10 | s.homepage = "https://github.com/nathanpc/ascii-image"
11 |
12 | s.add_runtime_dependency "rainbow", "~> 3.0", ">= 3.0.0"
13 | s.add_runtime_dependency "rmagick", "~> 2.16", ">= 2.16.0"
14 | end
15 |
--------------------------------------------------------------------------------
/examples/just-print-it.rb:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 |
3 | require 'rubygems'
4 | require '../lib/ascii-image.rb'
5 |
6 | ascii = ASCII_Image.new("poster.jpg")
7 | ascii.build(80)
8 |
--------------------------------------------------------------------------------
/examples/poster.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/nathanpc/ascii-image/5f129ac372699e05e92a4232e55fee30365e3504/examples/poster.jpg
--------------------------------------------------------------------------------
/lib/ascii-image.rb:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 |
3 | require 'rubygems'
4 | require 'rmagick'
5 | require 'rainbow'
6 | require 'open-uri'
7 |
8 | # == Summary
9 | #
10 | # This handy Ruby gem will help you to create awesome ASCII art from images
11 | # for your awesome command-line projects.
12 | #
13 | # == Example
14 | #
15 | # ascii = ASCII_Image.new("file.jpg")
16 | # ascii.build(80)
17 | #
18 | # == Contact
19 | #
20 | # Author:: Nathan Campos (nathanpc@dreamintech.net)
21 | # Website:: http://about.me/nathanpc
22 |
23 | class ASCII_Image
24 | # Initialize the ASCII_Image class.
25 | #
26 | # An Error is raised if your ImageMagick quantum depth is higher than 8.
27 | #
28 | # Arguments:
29 | # uri: (String)
30 | # console_width: (Integer)
31 |
32 | def initialize(uri, console_width = 80)
33 | @uri = uri
34 | @console_width = console_width
35 | @quantum_conversion_factor = 1
36 |
37 | if Magick::MAGICKCORE_QUANTUM_DEPTH > 16
38 | 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."
39 | elsif Magick::MAGICKCORE_QUANTUM_DEPTH == 16
40 | @quantum_conversion_factor = 257
41 | end
42 | end
43 |
44 | # Convert the image into ASCII and print it to the console.
45 | #
46 | # An ArgumentError is raised if the +width+ is bigger than the +console_width+
47 | #
48 | # Arguments:
49 | # width: (Integer)
50 |
51 | def build(width)
52 | # Open the resource
53 | resource = open(@uri)
54 | image = Magick::ImageList.new
55 | image.from_blob resource.read
56 |
57 | # Resize to the desired "text-pixel" size
58 | if width > @console_width
59 | raise ArgumentError, "The desired width is bigger than the console width"
60 | end
61 |
62 | image = image.scale(width / image.columns.to_f)
63 | # Compensate the height of the console "text-pixel"
64 | image = image.scale(image.columns, image.rows / 1.7)
65 |
66 | # Get the pixel array
67 | image.each_pixel do |pixel, col, row|
68 | print Rainbow(" ").background(pixel.red/@quantum_conversion_factor, pixel.green/@quantum_conversion_factor, pixel.blue/@quantum_conversion_factor)
69 |
70 | if (col % (width - 1) == 0) and (col != 0)
71 | print "\n"
72 | end
73 | end
74 | end
75 | end
76 |
--------------------------------------------------------------------------------