Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 18902a9

Browse files
Merge pull request avinashkranjan#1458 from aryanrai2001/Photo-To-Ascii-Improvements
Fixed bugs and improved output resolution
2 parents 9f022ac + 0257125 commit 18902a9

File tree

2 files changed

+31
-44
lines changed

2 files changed

+31
-44
lines changed

‎Photo To Ascii/README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11

2-
<h1 align="center">Photo to Ascii</h1>
3-
Convert your photo to ascii with it
2+
<h1 align="center">Photo to Ascii</h1>
3+
Convert your image to ASCII art using this simple python script!
44

5-
---------------------------------------------------------------------
5+
---
66

77
## Modules Used
88
- pillow
99

10-
1110
## How it works
12-
- First you have to enter the correct path where your photp is located.
13-
- Then it will resize your photo and grayify it
14-
- Then it will convert your photo to ascii
15-
- Then you could find your photo in {image_name}(ASCII).txt file where image_name is the name of your photo
11+
- First you have to enter the correct path where your image is located.
12+
- Then it will resize your image and grayify it.
13+
- Then it will convert your image to ascii.
14+
- Then it will save the ascii art to the file {imageFile-name}_{imageFile-extension}_ASCII.txt, located in the same folder as your image.
1615

17-
#### By [Avishake Maji](https://github.com/Avishake007)
16+
#### Created By [Avishake Maji](https://github.com/Avishake007)
17+
#### Improved By [Aryan Rai](https://github.com/aryanrai2001)

‎Photo To Ascii/photo_to_ascii.py

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,49 @@
11
from PIL import Image
22

3-
# ascii characters used to build the output text
3+
# ASCII characters used to build the output text
44
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-
145

6+
# Convert pixels to a string of ASCII characters
157
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])
1810
return(characters)
1911

20-
2112
def photoascii():
22-
23-
# attempt to open image from user-input
13+
# Attempt to open image file from user-input
2414
path = input("Enter a valid pathname to an image:\n")
2515
try:
2616
image = Image.open(path)
2717
except Exception:
2818
print("Invalid path")
2919
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
4229
new_width = 100
4330
width, height = image.size
4431
ratio = height/width
4532
new_height = int(new_width * ratio)
4633
resized_image = image.resize((new_width, new_height))
4734

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)
5137
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)])
5441

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:
5744
f.write(ascii_image)
5845

5946

60-
# run program
47+
# Run Program
6148
if __name__ == '__main__':
6249
photoascii()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /