image2ascii 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/image2ascii +58 -0
- data/lib/image2ascii.rb +82 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 71a51c97ec1912badd506a37658d8187d74443cefafea2c1682192827185fb22
|
4
|
+
data.tar.gz: a454214604deff3235a94750af750c868db33fc0c9c19b1fcd610dae8d08e40d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9012fe24c4a8888f22a5902921c5cabe047bc7c71d16df26f0520769c5f7576b229dfba09b7a58df8c9a121be9a7ff3d8ae78befda3cf0379125622ecdbf2c67
|
7
|
+
data.tar.gz: 220eaf2e542d95f1acae107933fa571b0f13c89592dda98dce08461997252be7019b8b026b695bd5d8a0c3dcd63dac80cc40b52db476c9a3625a1b0ac9879245
|
data/bin/image2ascii
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'optparse'
|
3
|
+
require 'image2ascii'
|
4
|
+
|
5
|
+
options = {}
|
6
|
+
OptionParser.new do |parser|
|
7
|
+
parser.banner = "Usage: image2ascii [image] [options]"
|
8
|
+
|
9
|
+
parser.on("-w", "--width N", Integer, "Width (in characters)") do |w|
|
10
|
+
options[:width] = w
|
11
|
+
end
|
12
|
+
|
13
|
+
parser.on("-t", "--terminal-percent N", Integer, "Width as percent of terminal size") do |t|
|
14
|
+
options[:term_width] = t
|
15
|
+
end
|
16
|
+
|
17
|
+
parser.on("-c COLOR", "--color COLOR", "Color in hex, rgb, or by name [examples: 'red', '255, 255, 255', '#FFFFFF']") do |c|
|
18
|
+
if c.include? ","
|
19
|
+
options[:color] = c.split(",").map{|x| x.to_i}
|
20
|
+
elsif c.include? "#"
|
21
|
+
options[:color] = c
|
22
|
+
else
|
23
|
+
options[:color] = c.to_sym
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
parser.on("-x CHARS", "--custom CHARS", "Custom characters in order of descending relative brightness [example: '@#-.']") do |x|
|
28
|
+
options[:chars] = x
|
29
|
+
end
|
30
|
+
|
31
|
+
parser.on("-i", '--invert', "Invert ASCII character order") do |i|
|
32
|
+
options[:invert] = true
|
33
|
+
end
|
34
|
+
|
35
|
+
parser.on("-b", "--block", "Print ASCII in block form") do |b|
|
36
|
+
options[:block] = true
|
37
|
+
end
|
38
|
+
|
39
|
+
parser.on("-g", "--greyscale", "Print ASCII in greyscale") do |b|
|
40
|
+
options[:color] = "greyscale"
|
41
|
+
end
|
42
|
+
|
43
|
+
parser.on("-h", "--help", "Prints this help") do
|
44
|
+
puts parser
|
45
|
+
exit
|
46
|
+
end
|
47
|
+
|
48
|
+
end.parse!
|
49
|
+
|
50
|
+
ascii = Image2ASCII.new(ARGV.pop)
|
51
|
+
options[:width] = (ascii.winsize * (options[:term_width].to_f / 100)).to_i if options[:term_width]
|
52
|
+
ascii.chars = options[:chars] if options[:chars]
|
53
|
+
ascii.chars = ascii.chars.reverse if options[:invert]
|
54
|
+
ascii.generate({
|
55
|
+
:width => options[:width],
|
56
|
+
:color => options[:color],
|
57
|
+
:block => options[:block]
|
58
|
+
})
|
data/lib/image2ascii.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'io/console'
|
3
|
+
require 'rmagick'
|
4
|
+
require 'rainbow'
|
5
|
+
require 'open-uri'
|
6
|
+
|
7
|
+
|
8
|
+
class Image2ASCII
|
9
|
+
|
10
|
+
attr_reader :winsize
|
11
|
+
attr_accessor :chars
|
12
|
+
|
13
|
+
def initialize(uri)
|
14
|
+
@uri = uri
|
15
|
+
@winsize = IO.console.winsize[1]
|
16
|
+
@chars = %($@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,"^`'.).reverse
|
17
|
+
#quantum conversion factor for dealing with quantum depth color values
|
18
|
+
@qcf = 1
|
19
|
+
|
20
|
+
if Magick::MAGICKCORE_QUANTUM_DEPTH > 16
|
21
|
+
raise "ImageMagick quantum depth is set to #{Magick::MAGICKCORE_QUANTUM_DEPTH}. It needs to be 16 or less"
|
22
|
+
elsif Magick::MAGICKCORE_QUANTUM_DEPTH == 16
|
23
|
+
#divides quantum depth color space into useable rgb values
|
24
|
+
@qcf = 257
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def generate(args={})
|
29
|
+
|
30
|
+
args[:width] ||= @winsize
|
31
|
+
args[:color] ||= "full"
|
32
|
+
args[:hidden] ||= false
|
33
|
+
args[:block] ||= false
|
34
|
+
as_string = ""
|
35
|
+
|
36
|
+
#load the image
|
37
|
+
resource = URI.open(@uri)
|
38
|
+
img = Magick::ImageList.new
|
39
|
+
img.from_blob(resource.read)
|
40
|
+
|
41
|
+
#correct aspect ratio
|
42
|
+
img = img.scale(args[:width] / img.columns.to_f)
|
43
|
+
img = img.scale(img.columns, img.rows / 2)
|
44
|
+
|
45
|
+
img.each_pixel do |pixel, col, row|
|
46
|
+
|
47
|
+
#get RGB values and brightness of pixels
|
48
|
+
r = pixel.red / @qcf
|
49
|
+
g = pixel.green / @qcf
|
50
|
+
b = pixel.blue / @qcf
|
51
|
+
brightness = (0.2126*r + 0.7152*g + 0.0722*b)
|
52
|
+
#select from chars that are already pre-ordered in brightness
|
53
|
+
char_index = brightness / (255.0 / @chars.length)
|
54
|
+
char = @chars[char_index.floor]
|
55
|
+
as_string << char
|
56
|
+
|
57
|
+
chosen_color =
|
58
|
+
if args[:color] == "full"
|
59
|
+
[r, g, b]
|
60
|
+
elsif args[:color] == "greyscale"
|
61
|
+
[brightness, brightness, brightness]
|
62
|
+
else
|
63
|
+
args[:color]
|
64
|
+
end
|
65
|
+
|
66
|
+
if args[:block]
|
67
|
+
print Rainbow(" ").background(*chosen_color) if !args[:hidden]
|
68
|
+
else
|
69
|
+
print Rainbow(char).color(*chosen_color) if !args[:hidden]
|
70
|
+
end
|
71
|
+
|
72
|
+
#add line wrap once desired width is reached
|
73
|
+
if (col % (args[:width] - 1) == 0) and (col != 0)
|
74
|
+
print "\n" if !args[:hidden]
|
75
|
+
as_string << "\n"
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
return as_string
|
81
|
+
end
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: image2ascii
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.4
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Kofron
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: An RMagick-based ASCII art generator that allows custom characters, colors,
|
14
|
+
ands widths. Can also detect terminal window size for responsive output. Use with
|
15
|
+
code or with or in the command-line.
|
16
|
+
email: kofronmichael@gmail.com
|
17
|
+
executables:
|
18
|
+
- image2ascii
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- bin/image2ascii
|
23
|
+
- lib/image2ascii.rb
|
24
|
+
homepage: https://rubygems.org/gems/image2ascii
|
25
|
+
licenses:
|
26
|
+
- MIT
|
27
|
+
metadata:
|
28
|
+
source_code_uri: https://github.com/michaelkofron/image2ascii
|
29
|
+
documentation_uri: https://github.com/michaelkofron/image2ascii#image2ascii
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.2.3
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Convert images to custom ASCII art with code or from the command-line
|
49
|
+
test_files: []
|