|
1 | 1 | from PIL import Image, ImageEnhance
|
2 | 2 |
|
3 | 3 | # ASCII characters used to build the output text
|
4 |
| -CHARS = "00011111..." |
| 4 | +char_ramp = [] |
| 5 | + |
| 6 | +# Choose character sequence for mapping |
| 7 | +def set_char_ramp(): |
| 8 | + global char_ramp |
| 9 | + print("Choose a Character Sequence!") |
| 10 | + print("1 - Basic") |
| 11 | + print("2 - Standard (Short)") |
| 12 | + print("3 - Standard (Long)") |
| 13 | + print("4 - Unicode Blocks") |
| 14 | + print("5 - Unicode Shades") |
| 15 | + print("6 - Enter Custom Sequence") |
| 16 | + choice = input("Input: ") |
| 17 | + |
| 18 | + try: |
| 19 | + choice = int(choice) |
| 20 | + except Exception: |
| 21 | + print("Invalid Input!") |
| 22 | + exit() |
| 23 | + |
| 24 | + if choice == 1: |
| 25 | + char_ramp = list("00011111...") |
| 26 | + elif choice == 2: |
| 27 | + char_ramp = list("@%#*+=-:. ") |
| 28 | + elif choice == 3: |
| 29 | + char_ramp = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1}{[]?-_+~<>i!lI;:,\"^`'. ") |
| 30 | + elif choice == 4: |
| 31 | + char_ramp = ["█", "▉", "▊", "▋", "▌", "▍", "▎", "▏"] |
| 32 | + elif choice == 5: |
| 33 | + char_ramp = ["█", "▓", "▒", "░", " "] |
| 34 | + elif choice == 6: |
| 35 | + custom_ramp = input("Enter your Character Sequence from High density to Low: ") |
| 36 | + if len(custom_ramp) == 0: |
| 37 | + print("Invalid Input!") |
| 38 | + exit() |
| 39 | + char_ramp = list(custom_ramp) |
| 40 | + else: |
| 41 | + print("Invalid Input!") |
| 42 | + exit() |
| 43 | + |
5 | 44 |
|
6 | 45 | # Convert pixels to a string of ASCII characters
|
7 | 46 | def pixels_to_ascii(image, contrast_factor):
|
8 | 47 | image = image.convert("L")
|
9 | 48 | enhancer = ImageEnhance.Contrast(image)
|
10 | 49 | image = enhancer.enhance(contrast_factor)
|
11 | 50 | pixels = image.getdata()
|
12 |
| - characters = " ".join([CHARS[int((pixel/256)*len(CHARS))] for pixel in pixels]) |
| 51 | + characters = " ".join([char_ramp[int((pixel/256)*len(char_ramp))] for pixel in pixels]) |
13 | 52 | return(characters)
|
14 | 53 |
|
| 54 | +# Driver function |
15 | 55 | def photoascii():
|
16 | 56 | # Attempt to open image file from user-input
|
17 | 57 | path = input("Enter a valid pathname to an image:\n")
|
18 | 58 | try:
|
19 | 59 | image = Image.open(path)
|
20 | 60 | except Exception:
|
21 | 61 | print("Invalid Path!")
|
22 |
| - return |
| 62 | + exit() |
23 | 63 |
|
24 | 64 | contrast_factor = input("Enter contrast factor (1 = Original Contrast) [Note: Enter negative value to invert output] : ")
|
25 | 65 | try:
|
26 | 66 | contrast_factor = float(contrast_factor)
|
27 | 67 | except Exception:
|
28 | 68 | print("Invalid Input!")
|
29 |
| - return |
30 |
| - |
| 69 | + exit() |
| 70 | + |
| 71 | + set_char_ramp() |
| 72 | + |
31 | 73 | if contrast_factor < 0:
|
32 |
| - global CHARS |
33 |
| - CHARS = CHARS[::-1] |
| 74 | + global char_ramp |
| 75 | + char_ramp.reverse() |
34 | 76 | contrast_factor = -contrast_factor
|
35 |
| - |
| 77 | + |
36 | 78 | # Fetch the name of the image file
|
37 | 79 | dot_index = path.rfind('.')
|
38 | 80 | slash_index = path.rfind('\\')
|
39 | 81 | if slash_index == -1:
|
40 | 82 | slash_index = path.rfind('/')
|
41 | 83 | image_name = path[slash_index+1:dot_index] + "_" + path[dot_index+1:]
|
42 |
| - |
| 84 | + |
43 | 85 | # Resize image
|
44 | 86 | new_width = 100
|
45 | 87 | width, height = image.size
|
46 | 88 | ratio = height/width
|
47 | 89 | new_height = int(new_width * ratio)
|
48 | 90 | resized_image = image.resize((new_width, new_height))
|
49 |
| - |
| 91 | + |
50 | 92 | # Convert image to ASCII
|
51 | 93 | new_image_data = pixels_to_ascii(resized_image, contrast_factor)
|
52 | 94 | pixel_count = len(new_image_data)
|
53 | 95 | scanline_width = new_width * 2;
|
54 | 96 | ascii_image = "\n".join([new_image_data[index:(index+scanline_width)]
|
55 | 97 | for index in range(0, pixel_count, scanline_width)])
|
56 |
| - |
| 98 | + |
57 | 99 | # Save result to text file
|
58 |
| - with open(path[:slash_index] + "/{}_ASCII.txt".format(image_name), "w") as f: |
| 100 | + with open(path[:slash_index] + "/{}_ASCII.txt".format(image_name), "w", encoding='utf8') as f: |
59 | 101 | f.write(ascii_image)
|
60 | 102 |
|
61 | 103 |
|
|
0 commit comments