photo to char
Recently, a project called "Picture-to-Character Painting" is really fun. This time I will take you to follow suit.
Ideas
I’m lazy and I’m not on the flow chart anymore. Let’s talk about it roughly
- The user enters the path of the picture to be converted, the storage path, and the specifications (length, width) of the "character painting" stored
- Get image grayscale
- Select characters by grayscale value
- storage
achieve
Import PIL.Image
from PIL import Image
Get the data entered by the user
inPath = input('path for process:')
WIDTH = int(input('How long you want for width:'))
HEIGHT = int(input('How long you want for height:'))
outPath = input('path for save:')
print('processing,please wait')
Define the characters corresponding to different gray levels in the picture
Due to the rush to write a blog post, there are fewer defined characters, so please don’t spray
char = u'WM%&@-.'
The function "limit" and the value "demarcation" defined for the "getchar" function defined later
demarcation = 128
def limit(x,start,end):
if(x <= start):
return start
if(x >= end):
return end
return x
Get the character form of an rgb value (function "getchar")
def getchar(r,g,b):
gray = float(0.2126 * r + 0.7152 * g + 0.0722 * b)
std = int(demarcation / len(char))
return char[limit(int(gray / std),0,len(char) - 1)]
Main program
if(__name__ == '__main__'):
img = Image.open(inPath).convert('RGB')
img = img.resize((WIDTH,HEIGHT),Image.NEAREST)
text = ''
for i in range(HEIGHT):
for j in range(WIDTH):
r,g,b = img.getpixel((j,i))
text += getchar(r,g,b)
text += '\n'
with open(outPath,'w') as file:
file.write(text)
If you don’t understand the program, just comment below, someone should answer
test
Attach the source code and github link here
Source code:
from PIL import Image
inPath = input('path for process:')
WIDTH = int(input('How long you want for width:'))
HEIGHT = int(input('How long you want for height:'))
outPath = input('path for save:')
print('processing,please wait')
char = u'WM%&@-.'
demarcation = 128
def limit(x,start,end):
if(x <= start):
return start
if(x >= end):
return end
return x
def getchar(r,g,b):
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
std = float(demarcation / len(char))
return char[limit(int(gray / std),0,len(char) - 1)]
if(__name__ == '__main__'):
img = Image.open(inPath).convert('RGB')
img = img.resize((WIDTH,HEIGHT),Image.NEAREST)
text = ''
for i in range(HEIGHT):
for j in range(WIDTH):
r,g,b = img.getpixel((j,i))
text += getchar(r,g,b)
text += '\n'
with open(outPath,'w') as file:
file.write(text)
test
input the command
(base) xuans-mbp:~ [马赛克] $ python main.py
path for process: a.jpg
How long you want for width: 247
How long you want for height: 101
path for save:a.txt
**
Output
It's a bit unsightly, but it's much better after adjusting the "char" parameter.
Modify:
char = u'WMBRENHFPAGKSZVXQCLUODIJTY0896543721mwqpdgbeanuskxyzfdhtocvrijl#@&$%}{][)(?=;!<>/\|-:*+~·'
demarcation = 250
references
no
Copyright Notice
The copyright belongs to the author of this article's reference and himself, please declare the source (if you are too lazy to write it, you don't see it, don't pursue it, you just know it)
Author
hit-road
Bye, get out of class is over!