-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_text.py
56 lines (42 loc) · 1.5 KB
/
text_to_text.py
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
55
56
import os
import json
import argparse
FONT_FILE = 'font6.json'
with open(f'fonts/{FONT_FILE}', 'r') as f:
char_map = json.load(f)
def string_to_lines(s):
height = char_map["height"]
# Initialize a list of lines with empty strings
lines = ['' for _ in range(height)]
# Iterate over the characters in the string
for char in s:
# Look up the larger representation of the character
char_lines = char_map[char]
# Append the lines of the larger representation to the lines list
for i in range(height):
lines[i] += char_lines[i]
return lines
def write_string_to_file(str1):
# Convert the string to lines
lines = string_to_lines(str1)
# Create a filename based on the first five characters of the input string
filename = 'text_output/' + str1[:5] + '.txt'
# Create the output_txt directory if it doesn't exist
os.makedirs(os.path.dirname(filename), exist_ok=True)
# Open the file
with open(filename, 'w') as f:
# Write the lines to the file
for line in lines:
f.write(line + '\n')
import os
def main():
parser = argparse.ArgumentParser(description='Convert a string to ASCII art.')
parser.add_argument('input_str', nargs='?', help='The string to convert to ASCII art.')
args = parser.parse_args()
if args.input_str:
input_str = args.input_str
else:
input_str = input('Enter a string: ')
write_string_to_file(input_str)
if __name__ == '__main__':
main()