ascii-image 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/ascii-image.rb +71 -0
  2. metadata +46 -0
@@ -0,0 +1,71 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'RMagick'
5
+ require 'rainbow'
6
+
7
+ # == Summary
8
+ #
9
+ # This handy Ruby gem will help you to create awesome ASCII art from images
10
+ # for your awesome command-line projects.
11
+ #
12
+ # == Example
13
+ #
14
+ # ascii = ASCII_Image.new("file.jpg")
15
+ # ascii.build(80)
16
+ #
17
+ # == Contact
18
+ #
19
+ # Author:: Nathan Campos (nathanpc@dreamintech.net)
20
+ # Website:: http://about.me/nathanpc
21
+
22
+ class ASCII_Image
23
+ # Initialize the ASCII_Image class.
24
+ #
25
+ # An Error is raised if your ImageMagick quantum depth is higher than 8.
26
+ #
27
+ # Arguments:
28
+ # file: (String)
29
+ # console_width: (Integer)
30
+
31
+ def initialize(file, console_width = 80)
32
+ @file = file
33
+ @console_width = console_width
34
+
35
+ if Magick::QuantumDepth > 8
36
+ 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."
37
+ end
38
+ end
39
+
40
+ # Convert the image into ASCII and print it to the console.
41
+ #
42
+ # An ArgumentError is raised if the +width+ is bigger than the +console_width+
43
+ #
44
+ # Arguments:
45
+ # width: (Integer)
46
+
47
+ def build(width)
48
+ # Open the image file
49
+ image = Magick::Image.read(@file)[0]
50
+
51
+ # Resize to the desired "text-pixel" size
52
+ if width > @console_width
53
+ raise ArgumentError, "The desired width is bigger than the console width"
54
+ end
55
+
56
+ image = image.scale(width / image.columns.to_f)
57
+ # Compensate the height of the console "text-pixel"
58
+ image = image.scale(image.columns, image.rows / 1.7)
59
+
60
+ # Get the pixel array
61
+ image.each_pixel do |pixel, col, row|
62
+ print " ".background(pixel.red, pixel.green, pixel.blue)
63
+
64
+ if (col % (width - 1) == 0) and (col != 0)
65
+ print "\n"
66
+ end
67
+ end
68
+
69
+ print "\n"
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascii-image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Campos
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A Ruby gem to convert images into ASCII for your awesome command-line
15
+ applications
16
+ email: nathanpc@dreamintech.net
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/ascii-image.rb
22
+ homepage: https://github.com/nathanpc/ascii-image
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.11
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: Convert images into ASCII
46
+ test_files: []