summaryrefslogtreecommitdiffstats
path: root/text_art/idk.py
blob: bee31a513c2c1ad9ab8c8234d32c2d9efbc42d87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from PIL import Image # pip install Pillow
from math import floor


def find_index(num, min_num, max_num, min_range, max_range):
    n = num / (max_num - min_num + 1)
    return (max_range - min_range + 1) * n + min_range


# file_name = input()
img = Image.open("C:/Users/admon/PycharmProjects/coding_club/text_art/garfield.png")
img = img.convert("RGB")

width, height = img.size
resize = 0.5
img = img.resize((floor(width * resize), floor(height * resize)))
width, height = img.size

pix = img.load()

density = "Ñ@#W$9876543210?!abc;:+=-,._                    "
density = density[::-1]
# density = '       .:-i|=+%O#@'

density_length = len(density)

text_art = ""

for y in range(height):
    for x in range(width):
        r = pix[x, y][0]
        g = pix[x, y][1]
        b = pix[x, y][2]
        # r,g,b = pix[x,y]
        avg = (r + g + b) / 3
        density_index = floor(find_index(avg, 0, 255, 0, density_length - 1))
        text_art += density[-density_index - 1]
    text_art += '\n'
# img.show()

with open('output.txt', 'w', encoding='utf-8') as f:
    f.write(text_art)