Search code examples
pythonhex

Trasforming pixel art into ASCII art


I am trying to do something difficult because I'm a bit insane. I am making a text/ASCII adventure game in python for my introduction to programing class. I have a few of the systems I will be using made already but I want to make sure I can create new content as needed in a relatively short time.

One of the key features of this game will be its battle maps. Using ASCII symbols to represent terrain and peaces on a board (kinda like DND) I can make several 15x10 maps. However I find that creating these maps is time consuming and annoying. Something I want to do is take an image like the one I hope I managed to attach and look at its hex code in python. From there have a script that looks at each colored pixel and put a ASCII character into that same spot on a text file.

The goal is to get the image attached(This example is made larger and the white pixels have been darkened) to look something like the following. An image of several colored pixels with a white background

░|░|░|░|░|░|░|░|░|░|░|░|░|░|░
░|░|░|░|░|░|░|░|░|░|░|░|░|░|▒
░|░|░|░|░|░|░|░|░|░|░|░|░|░|▓
░|░|░|░|░|░|░|░|░|░|░|░|░|░|@
░|░|░|░|░|░|░|░|░|░|░|░|░|░|¥
░|░|░|░|░|░|░|░|░|░|░|░|░|░|E
░|░|░|░|░|░|░|░|░|░|░|░|░|░|w
░|░|░|░|░|░|░|░|░|░|░|░|░|░|s
░|░|░|░|░|░|░|░|░|░|░|░|░|░|z
░|░|░|░|░|░|░|░|░|░|░|░|░|░|░

I know this isn't going to be easy but I just need a break down / guidance of two things.

  1. What is the make up of an image when looking at it in a HEX editor HEX of the attached image
89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 00 00 00 0F 00 00 00 0A 08 06 00 00 00 6B 1B 04 F9 00 00 00 40 49 44 41 54 28 53 63 FC 0F 04 0C 64 02 46 72 35 6F DD BA 95 81 6C CD EE EE EE E4 6B 2E 30 9A 49 BE 66 E5 4D BE E4 6B 9E C5 68 48 81 E6 5B AF C9 D7 CC B8 E3 03 F9 9A FD F2 BE 90 AF 19 94 AE 00 38 F2 33 B8 0F CC E8 54 00 00 00 00 49 45 4E 44 AE 42 60 82
  1. What resources python has to read these types of files.

I am aware that python can use the open(), write(), and read() functions to make it possible to look at the file as well as edit files. It's easy with text files and I've been doing that alot with this being an RPG I'm making. Lots of dialog and stats to keep track of.


Solution

  • You can use PIL library to read image pixel-by-pixel and then convert it to ASCII art:

    from PIL import Image
    
    ITEMS = {(194, 194, 194, 255): '░',
             (180, 180, 180, 255): '▒',
             (70, 70, 70, 255): '▓',
             (111, 49, 152, 255): '@',
             (34, 177, 76, 255): '¥',
             (153, 0, 48, 255): 'E',
             (153, 217, 234, 255): 'w',
             (0, 183, 239, 255): 's',
             (77, 109, 243, 255): 'z'}
    
    image = Image.open('image.png')
    width, height = image.size
    pix = image.load()
    for y in range(0, height, 50):
        line = ''
        for x in range(0, width, 50):
            line += ITEMS[pix[x, y]] + '|'
        print(line[:-1])
    

    Output:

    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|░
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|▒
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|▓
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|@
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|¥
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|E
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|w
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|s
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|z
    ░|░|░|░|░|░|░|░|░|░|░|░|░|░|░