How to draw characters from pictures in the python series from scratch

  • 2020-04-02 13:42:16
  • OfStack

Here's the idea:

The original image - > Gray - > Maps to the specified sequence of characters - according to the pixel brightness - > The output.
The more characters, the denser the characters. It works better.
It is better to make character drawings according to the pixel brightness range of grayscale.
If you use the palette again and change the color of the characters, it will look more like the original.

Here's the original:  

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/2014523111317117.png? 2014423111331 ">

This is the generated character drawing:

< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/2014523111346883.png? 201442311142 ">

No more nonsense, direct on the code:


import Image
chars =" ...',;:clodxkLO0DGEKNWMM"
fn=r'c:usersliabcdesktopjianbing.png'
f1=lambda F:''.join([(k%100!=0) and m or m+'n' for k,m in enumerate(apply(lambda x:[chars[x[j,i]%len(chars)] for i in xrange(70) for j in xrange(100)],(Image.open(F).resize((100,70)).convert("L").load(),)),1)])
 f=open(r"c:usersliabcdesktopaface.txt","w")
f.write(f1(fn))
f.close()


Related articles: