1
- '''
1
+ """
2
2
Script that converts any Image into its ASCII representation.
3
- '''
3
+ """
4
4
5
5
from sys import exit as sysexit
6
6
from PIL import Image , ImageEnhance
7
7
8
8
9
9
def pixels_to_ascii (image , char_ramp ):
10
- '''
11
- Function that takes in an image and a character sequence. And returns a string containing
12
- the ASCII representation of the image based on the character sequence provided.
13
- '''
10
+ """
11
+ Function that takes in an image and a character sequence.
12
+ And returns a string containing the ASCII representation of
13
+ the image based on the character sequence provided.
14
+ """
14
15
15
16
pixels = image .convert ("L" ).getdata ()
16
- characters = " " .join ([char_ramp [int ((pixel / 256 )* len (char_ramp ))] for pixel in pixels ])
17
+ characters = " " .join (
18
+ [char_ramp [int ((pixel / 256 ) * len (char_ramp ))] for pixel in pixels ]
19
+ )
17
20
pixel_count = len (characters )
18
21
scanline_width = image .width * 2
19
22
20
- return "\n " .join ([characters [index :(index + scanline_width )]
21
- for index in range (0 , pixel_count , scanline_width )])
23
+ return "\n " .join (
24
+ [
25
+ characters [index : (index + scanline_width )]
26
+ for index in range (0 , pixel_count , scanline_width )
27
+ ]
28
+ )
22
29
23
30
24
31
def input_image ():
25
- '''
26
- Function that asks user for a path to an image file and checks for validity.
27
- Then it loads and returns the image and the path as a tuple(image, path).
28
- '''
32
+ """
33
+ Function that asks user for a path to an image file
34
+ and checks for validity. Then it loads and returns the
35
+ image and the path as a tuple(image, path).
36
+ """
29
37
30
38
path = input ("Enter a valid pathname to an image:\n " )
31
39
@@ -39,10 +47,11 @@ def input_image():
39
47
40
48
41
49
def input_ramp ():
42
- '''
43
- Function that asks the user to choose from a set of character sequences
44
- or to specify their own custom sequence and then returns that as a string.
45
- '''
50
+ """
51
+ Function that asks the user to choose from a set
52
+ of character sequences or to specify their own
53
+ custom sequence and then returns that as a string.
54
+ """
46
55
47
56
print ("Choose a Character Sequence!" )
48
57
print ("1 - Basic" )
@@ -64,13 +73,25 @@ def input_ramp():
64
73
if choice == 2 :
65
74
return list ("@%#*+=-:. " )
66
75
if choice == 3 :
67
- return list ("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\ |()1}{[]?-_+~<>i!lI;:,\" ^`'. " )
76
+ return list (
77
+ "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft"
78
+ + "/\\ |()1}{[]?-_+~<>i!lI;:,\" ^`'. "
79
+ )
68
80
if choice == 4 :
69
81
return ["█" , "▉" , "▊" , "▋" , "▌" , "▍" , "▎" , "▏" ]
70
82
if choice == 5 :
71
83
return ["█" , "▓" , "▒" , "░" , " " ]
72
84
if choice == 6 :
73
- custom_ramp = input ("Enter your Character Sequence from High density to Low: " )
85
+ while True :
86
+ custom_ramp = input ("Enter character sequence ['?' for info]: " )
87
+ if custom_ramp == "?" :
88
+ print (
89
+ "The character sequence must start with characters" ,
90
+ "that represent high pixel density and end with" ,
91
+ "characters that represent low pixel density." ,
92
+ )
93
+ else :
94
+ break
74
95
if len (custom_ramp ) == 0 :
75
96
print ("Invalid Input!" )
76
97
sysexit ()
@@ -81,17 +102,19 @@ def input_ramp():
81
102
82
103
83
104
def input_contrast ():
84
- '''
105
+ """
85
106
Function that asks user for the contrast factor that is to be applied
86
107
on the image before conversion. And returns it.
87
- '''
108
+ """
88
109
89
110
while True :
90
- contrast_factor = input ("Enter contrast factor [Default - 1 | '?' for info] : " )
91
- if contrast_factor == '?' :
92
- print ("Contrast factor is a value that is used to controle the contrast of the output." ,
93
- "Default value of the contrast factor is 1." ,
94
- "Entering a negative value will invert the output." )
111
+ contrast_factor = input ("Enter contrast factor ['?' for info] : " )
112
+ if contrast_factor == "?" :
113
+ print (
114
+ "Contrast factor is a value that is used to controle" ,
115
+ "the contrast of the output. Default value of the contrast" ,
116
+ "factor is 1. Negative value will invert the output." ,
117
+ )
95
118
else :
96
119
break
97
120
@@ -105,18 +128,20 @@ def input_contrast():
105
128
106
129
107
130
def resize_image (image ):
108
- '''
131
+ """
109
132
Function that takes in an image and asks the user for a sample size.
110
- Then returns a resized image such that each pixel represents the sample grid.
111
- '''
133
+ Then returns a resized image with each pixel representing the sample grid.
134
+ """
112
135
113
136
while True :
114
- sample_size = input ("Enter the sample size [Default - 4 | '?' for info] : " )
115
- if sample_size == '?' :
116
- print ("Sample size refers to the number of pixels" ,
137
+ sample_size = input ("Enter the sample size ['?' for info] : " )
138
+ if sample_size == "?" :
139
+ print (
140
+ "Sample size refers to the number of pixels" ,
117
141
"that will be sampled for one character." ,
118
142
"Default value of sample size is 4." ,
119
- "Its value must be greater than or equal to 1." )
143
+ "Its value must be greater than or equal to 1." ,
144
+ )
120
145
else :
121
146
break
122
147
@@ -138,26 +163,26 @@ def resize_image(image):
138
163
139
164
140
165
def get_output_path (path ):
141
- '''
166
+ """
142
167
Function that takes in the path of the input image file and returns the
143
- path of a text file that the ouput will be saved to.
144
- '''
168
+ path of a text file that the ouput will be saved to.
169
+ """
145
170
146
- dot_index = path .rfind ('.' )
147
- slash_index = path .rfind (' \\ ' )
171
+ dot_index = path .rfind ("." )
172
+ slash_index = path .rfind (" \\ " )
148
173
149
174
if slash_index == - 1 :
150
- slash_index = path .rfind ('/' )
175
+ slash_index = path .rfind ("/" )
151
176
152
- image_name = path [slash_index + 1 : dot_index ] + "_" + path [dot_index + 1 :]
177
+ image_name = path [slash_index + 1 : dot_index ] + "_" + path [dot_index + 1 :]
153
178
154
179
return path [:slash_index ] + f"/{ image_name } _ASCII.txt"
155
180
156
181
157
182
def main ():
158
- '''
183
+ """
159
184
The main function.
160
- '''
185
+ """
161
186
162
187
image , path = input_image ()
163
188
char_ramp = input_ramp ()
@@ -170,8 +195,9 @@ def main():
170
195
image = resize_image (ImageEnhance .Contrast (image ).enhance (contrast_factor ))
171
196
ascii_image = pixels_to_ascii (image , char_ramp )
172
197
173
- with open (get_output_path (path ), "w" , encoding = ' utf8' ) as file :
198
+ with open (get_output_path (path ), "w" , encoding = " utf8" ) as file :
174
199
file .write (ascii_image )
175
200
176
- if __name__ == '__main__' :
201
+
202
+ if __name__ == "__main__" :
177
203
main ()
0 commit comments