DEV Community 👩‍💻👨‍💻

openHacking
openHacking

Posted on

Python Image to ASCII Online

Introduction

Recently I found a fun way to play Python, using Python to convert pictures into ASCII image, most of which are local programs, because I built an online version of Python before, so I tried to use the online version of Python to realize image-to-ASCII.

Online Python editor: https://lwebapp.com/en/python-playground

Analysis

The basic idea of image-to-ASCII painting is

  1. Use PIL to get the RGB color value of each pixel of the image
  2. Then map the RGB color value to a string
  3. Finally, just splicing the strings together, printing to the terminal or saving as text, you can see the ASCII image.

We are here for the convenience of demonstration, and use the print method to print characters uniformly.

Code

Since we want to present on a web page, we can't load the image locally, we choose to load the image directly from base64 encoding, or load it from a remote URL. Here we will introduce how to load images.

1. Use base64 images

Use regular expressions to remove the header information of the picture base64 encoded string, and then use the base64 method to decode, and BytesIO cooperates with PIL to open it as a PIL picture instance.

Here I recommend my Online Image to Base64 tool, which supports directly pasting images and converting them into base64 strings, which is very convenient

import base64
import re
from io import BytesIO
from PIL import Image

# random string
char = list('M3NB6Q#OC?7>!:–;. ')

# color value map string
def get_char(r, g, b, alpha=256):
    if alpha == 0:
        return ' '

    grey = (2126 * r + 7152 * g + 722 * b) / 10000

    char_idx = int((grey / (alpha + 1.0)) * len(char))
    return char[char_idx]

# image base64 string
img_base64 = '''data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAHR5JREFUeF7tnQvUPlVVxh9FES1CU9SCLmhKoHkBwbxLamgm2sULmFqCrZVkllqaWmpGeb+irWVgmgmplYK3zPsNTQJTC8VL1ApKRVOkC6FY6wdn/r7f+837zpmZvc85M3P2WrPeD/4z5+yzzzxzbns/+2qqUi1QLbDRAlertqkWqBbYbIEKkPp2VAtssUAFiP/rsa+km0i6kST+5vrulb/X/xuNLg3Xf678zf9b/e8vSfqn8O/+rVhoDRUgNh0PAA4KQFj/vYFNFRtL+UoAygUbfp2rn3fxFSDD+vfWkn5C0tHhGlZKmqfeIYnrPZI+kabK+dRSARLXlz8i6U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ksJVfsjLwkhCegAuAsMgTChJmMcTSERqBy5GjhIFl3XOOYr82JQMkKYzOUEFKN6RiWNfHtYp7wuUO+Rn5yLNcwohTTS5yrmgHrp7YheQIW0kEhBgeKcBH6LbnmemABCUZfEOSLxj3EcZs+Vhcms0YDkv/H2hpK+vXCxMtwkbFtdduQ4MQDh0BRTkOJmSEB4LOFzCZC0NMRWA0GZGEEDCiDInASCrgKFtq4DIvaNnbWtGDMBhTrBgrSjlTQkgTftZkwAUT3I6D1svvUzWGADDlLfK26hTBAg2YXcL1ndPmlNv2y+pfFhnYHE3YzxMZbypAqSxD35cUOh7scqn6oe51gPLOqkrvJk23ew3dYBgGFIvAJK5rU3cOj1Rwaw1AMfoFASJ9G2tZg4AaRqGcxs5Ez3SweXso6nVDdMlOQFzJ+cxsducANIYBJAwonhk3zUx+kwLgUSbEWNwwswS7TJHgGBnyASIX2aNUvoBY4nvRR+d2K5ljQHPwOzIMeYKkKaDDw5BPwBl/z69Xu/ttMDFARgEm53fefdEb5g7QJpuOSCMJkSokZujynALkKMEUDBqXDS8mGk8uRSANL2xXwAKIwoew1XiLYCnLaDguiT+sWnfuTSANL21V8h9fowkrqn5MqV66/AlOzNcEP5dkariUupZKkBW7c+UqwFK6RlrU703uO43wGBKtVipANnZ9YevgCUXk0eulxGGlwYU5+RSorR6K0A29wjBRhBuE4V3WGkdZ6TPuSEyEiJogr6qrFmgAiTulYD+hulXE7aaIo1DnGb97oKppQkXZhoF3VGVLRaoABn2epDv5B4r17BS0jz1bknN5Z5PI02T0tVSAWJja0gQWOy3/d7ApoqNpXwl8AyzmIZUYv3Xufp5F18B4t+/+wbgQK8D7xSA4XfThUbwcm26AAT/Bh0RgLjUvwnLraECZLl9X1seYYEKkAgj1VuWa4EKkOX2fW15hAX+H2d/zQWBav7aAAAAAElFTkSuQmCC'''

# Regularly delete header information, that is, Data URI scheme
image_data = re.sub('^data:image/.+;base64,', '', img_base64)
# open image
img = Image.open(BytesIO(base64.b64decode(image_data)))

img_widht = img.size[0]
img_height = img.size[1]

# set zoom ratio
scale_width = 0.3 # 0.75
scale_height = 0.1 # 0.5

# zoom image
img = img.resize((int(img_widht*scale_width),
                 int(img_height*scale_height)), Image.NEAREST)

# output ASCII image
text = ''
for i in range(int(img_height*scale_height)):
    for j in range(int(img_widht*scale_width)):
        text += get_char(*img.getpixel((j, i)))
    text += '\n'

print(text)
Enter fullscreen mode Exit fullscreen mode

Save the above code locally or copy it to Online Python Editor and run it to see the effect (Pillow needs to be installed before running)

2. Load image from URL

If it is a local application, we can use requests, a built-in Python library, to load network images. Since this online Python tool uses pyodide and does not support requests, we need to use the official API pyodide.http.pyfetch to implement the network ask

response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')
Enter fullscreen mode Exit fullscreen mode

After getting the image data, you can also open it with PIL. The detailed code is as follows

from io import BytesIO
from PIL import Image
import pyodide

char = list('M3NB6Q#OC?7>!:–;. ')


def get_char(r, g, b, alpha=256):
    if alpha == 0:
        return ' '

    grey = (2126 * r + 7152 * g + 722 * b) / 10000

    char_idx = int((grey / (alpha + 1.0)) * len(char))
    return char[char_idx]


def write_file(out_file_name, content):
    with open(out_file_name, 'w') as f:
        f.write(content)

# Python API provided by pyodide for network requests, reference: https://pyodide.org/en/stable/usage/api/python-api.html#pyodide.http.pyfetch
response = await pyodide.http.pyfetch('https://gcore.jsdelivr.net/gh/openHacking/static-files@main/img/16576149784751657614977527.png')

image_data = await response.bytes()
img = Image.open(BytesIO(image_data))

img_widht = img.size[0]
img_height = img.size[1]

scale_width = 0.3 # 0.75
scale_height = 0.1 # 0.5

img = img.resize((int(img_widht*scale_width),
                 int(img_height*scale_height)), Image.NEAREST)

text = ''
for i in range(int(img_height*scale_height)):
    for j in range(int(img_widht*scale_width)):
        text += get_char(*img.getpixel((j, i)))
    text += '\n'

print(text)
Enter fullscreen mode Exit fullscreen mode

Online Demo: Python Image to ASCII - URL Image

Conclusion

The above is my summary of the method of converting image to ASCII in Python, which can be run on web pages and shared with your friends. There may be some shortcomings, welcome to explore. There will be more interesting Python usages in the future, welcome to follow my updates.

Reference

Top comments (0)

🤔 Did you know?

 
🌚 Dark mode is available in Settings.