Skip to content

Commit b584547

Browse files
committed
Added info for character sequence
1 parent eff6ee7 commit b584547

File tree

1 file changed

+70
-44
lines changed

1 file changed

+70
-44
lines changed

Photo To Ascii/photo_to_ascii.py

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
1-
'''
1+
"""
22
Script that converts any Image into its ASCII representation.
3-
'''
3+
"""
44

55
from sys import exit as sysexit
66
from PIL import Image, ImageEnhance
77

88

99
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+
"""
1415

1516
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+
)
1720
pixel_count = len(characters)
1821
scanline_width = image.width * 2
1922

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+
)
2229

2330

2431
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+
"""
2937

3038
path = input("Enter a valid pathname to an image:\n")
3139

@@ -39,10 +47,11 @@ def input_image():
3947

4048

4149
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+
"""
4655

4756
print("Choose a Character Sequence!")
4857
print("1 - Basic")
@@ -64,13 +73,25 @@ def input_ramp():
6473
if choice == 2:
6574
return list("@%#*+=-:. ")
6675
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+
)
6880
if choice == 4:
6981
return ["█", "▉", "▊", "▋", "▌", "▍", "▎", "▏"]
7082
if choice == 5:
7183
return ["█", "▓", "▒", "░", " "]
7284
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
7495
if len(custom_ramp) == 0:
7596
print("Invalid Input!")
7697
sysexit()
@@ -81,17 +102,19 @@ def input_ramp():
81102

82103

83104
def input_contrast():
84-
'''
105+
"""
85106
Function that asks user for the contrast factor that is to be applied
86107
on the image before conversion. And returns it.
87-
'''
108+
"""
88109

89110
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+
)
95118
else:
96119
break
97120

@@ -105,18 +128,20 @@ def input_contrast():
105128

106129

107130
def resize_image(image):
108-
'''
131+
"""
109132
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+
"""
112135

113136
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",
117141
"that will be sampled for one character.",
118142
"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+
)
120145
else:
121146
break
122147

@@ -138,26 +163,26 @@ def resize_image(image):
138163

139164

140165
def get_output_path(path):
141-
'''
166+
"""
142167
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+
"""
145170

146-
dot_index = path.rfind('.')
147-
slash_index = path.rfind('\\')
171+
dot_index = path.rfind(".")
172+
slash_index = path.rfind("\\")
148173

149174
if slash_index == -1:
150-
slash_index = path.rfind('/')
175+
slash_index = path.rfind("/")
151176

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:]
153178

154179
return path[:slash_index] + f"/{image_name}_ASCII.txt"
155180

156181

157182
def main():
158-
'''
183+
"""
159184
The main function.
160-
'''
185+
"""
161186

162187
image, path = input_image()
163188
char_ramp = input_ramp()
@@ -170,8 +195,9 @@ def main():
170195
image = resize_image(ImageEnhance.Contrast(image).enhance(contrast_factor))
171196
ascii_image = pixels_to_ascii(image, char_ramp)
172197

173-
with open(get_output_path(path), "w", encoding='utf8') as file:
198+
with open(get_output_path(path), "w", encoding="utf8") as file:
174199
file.write(ascii_image)
175200

176-
if __name__ == '__main__':
201+
202+
if __name__ == "__main__":
177203
main()

0 commit comments

Comments
 (0)