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 91e787a

Browse files
Merge pull request avinashkranjan#855 from Avishake007/photo_to_ascii.py
Added Photo_to_Ascii
2 parents 3d3b65c + c090bdc commit 91e787a

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

‎Photo To Ascii/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
<h1 align="center"> Photo to Ascii</h1>
3+
Convert your photo to ascii with it
4+
5+
---------------------------------------------------------------------
6+
7+
## Modules Used
8+
- pillow
9+
10+
11+
## 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
16+
17+
#### By [Avishake Maji](https://github.com/Avishake007)

‎Photo To Ascii/photo_to_ascii.py

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

0 commit comments

Comments
(0)

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