-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIMG2ASCII.rb
54 lines (42 loc) · 1.46 KB
/
IMG2ASCII.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- Author: Ali -*-
# -*- Info: -*-
# Description: This Ruby script converts an image to ASCII art using the ChunkyPNG library.
# It takes an image file path as a command-line argument and generates a text file
# containing the ASCII representation of the image.
require 'chunky_png'
# Define the characters to represent different pixel intensity levels
ASCII_PIXELS = "@%#*+=-:. "
# Function to map pixel value to ASCII character
def pixel_to_ascii(pixel_value)
index = (pixel_value * (ASCII_PIXELS.length - 1) / 255).to_i
ASCII_PIXELS[index]
end
# Function to convert an image to ASCII art
def convert_to_ascii(image_path)
image = ChunkyPNG::Image.from_file(image_path)
width = image.width
height = image.height
ascii_art = []
height.times do |y|
row = ""
width.times do |x|
pixel = image[x, y]
luminance = (0.3 * ChunkyPNG::Color.r(pixel) + 0.59 * ChunkyPNG::Color.g(pixel) + 0.11 * ChunkyPNG::Color.b(pixel)).to_i
row += pixel_to_ascii(luminance)
end
ascii_art << row
end
ascii_art.join("\n")
end
# Main program
if ARGV.length != 1
puts "Usage: IMG2ASCII.rb <image_path>"
exit 1
end
image_path = ARGV[0]
ascii_art = convert_to_ascii(image_path)
output_filename = "#{File.basename(image_path, ".*")}_ascii_art.txt"
File.open(output_filename, "w") do |file|
file.puts ascii_art
end
puts "ASCII art saved to #{output_filename}"