|
1 | 1 | from PIL import Image
|
2 | 2 |
|
3 | | -# ascii characters used to build the output text |
| 3 | +# ASCII characters used to build the output text |
4 | 4 | CHARS = [".", ".", ".", "1", "1", "1", "1", "1", "0", "0", "0"]
|
5 | | -# convert each pixel to grayscale |
6 | | - |
7 | | - |
8 | | -def grayify(image): |
9 | | - grayscale_image = image.convert("L") |
10 | | - return(grayscale_image) |
11 | | - |
12 | | -# convert pixels to a string of ascii characters |
13 | | - |
14 | 5 |
|
| 6 | +# Convert pixels to a string of ASCII characters |
15 | 7 | def pixels_to_ascii(image):
|
16 | | - pixels = image.getdata() |
17 | | - characters = "".join([CHARS[pixel//23] for pixel in pixels]) |
| 8 | + pixels = image.convert("L").getdata() |
| 9 | + characters = "".join([CHARS[int((pixel/256)*len(CHARS))] for pixel in pixels]) |
18 | 10 | return(characters)
|
19 | 11 |
|
20 | | - |
21 | 12 | def photoascii():
|
22 | | - |
23 | | - # attempt to open image from user-input |
| 13 | + # Attempt to open image file from user-input |
24 | 14 | path = input("Enter a valid pathname to an image:\n")
|
25 | 15 | try:
|
26 | 16 | image = Image.open(path)
|
27 | 17 | except Exception:
|
28 | 18 | print("Invalid path")
|
29 | 19 | return
|
30 | | - # Fetching the name of the image |
31 | | - image_name = "" |
32 | | - flag = 0 |
33 | | - for i in path[::-1]: |
34 | | - if i == "/": |
35 | | - break |
36 | | - if flag == 1: |
37 | | - image_name = i+image_name |
38 | | - if i == '.': |
39 | | - flag = 1 |
40 | | - |
41 | | - # Resizing of image |
| 20 | + |
| 21 | + # Fetch the name of the image file |
| 22 | + dot_index = path.rfind('.') |
| 23 | + slash_index = path.rfind('\\') |
| 24 | + if slash_index == -1: |
| 25 | + slash_index = path.rfind('/') |
| 26 | + image_name = path[slash_index+1:dot_index] + "_" + path[dot_index+1:] |
| 27 | + |
| 28 | + # Resize image |
42 | 29 | new_width = 100
|
43 | 30 | width, height = image.size
|
44 | 31 | ratio = height/width
|
45 | 32 | new_height = int(new_width * ratio)
|
46 | 33 | resized_image = image.resize((new_width, new_height))
|
47 | 34 |
|
48 | | - # convert image to ascii |
49 | | - new_image_data = pixels_to_ascii(grayify(resized_image)) |
50 | | - |
| 35 | + # Convert image to ASCII |
| 36 | + new_image_data = pixels_to_ascii(resized_image) |
51 | 37 | pixel_count = len(new_image_data)
|
52 | | - ascii_image = "\n".join([new_image_data[index:(index+new_width)] |
53 | | - for index in range(0, pixel_count, new_width)]) |
| 38 | + scanline_width = new_width * 2; |
| 39 | + ascii_image = "\n".join([new_image_data[index:(index+scanline_width)] |
| 40 | + for index in range(0, pixel_count, scanline_width)]) |
54 | 41 |
|
55 | | - # save result to "ascii_image.txt" |
56 | | - with open("./Photo To Ascii/{}(ASCII).txt".format(image_name), "w") as f: |
| 42 | + # Save result to text file |
| 43 | + with open(path[:slash_index] +"/{}_ASCII.txt".format(image_name), "w") as f: |
57 | 44 | f.write(ascii_image)
|
58 | 45 |
|
59 | 46 |
|
60 | | -# run program |
| 47 | +# Run Program |
61 | 48 | if __name__ == '__main__':
|
62 | 49 | photoascii()
|
0 commit comments