60 lines
1.0 KiB
Python
60 lines
1.0 KiB
Python
import string
|
|
|
|
from PIL import Image
|
|
|
|
import pygame as pg
|
|
|
|
|
|
|
|
|
|
FONT_PATH = 'fonts/scientifica.ttf'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHARS = (string.ascii_letters + string.punctuation).replace('\\', '').replace('/', '').replace('|', '').replace('-', '')
|
|
sorted_chars = {}
|
|
|
|
for c in CHARS:
|
|
pg.init()
|
|
font = pg.font.Font(FONT_PATH, 12)
|
|
|
|
font_w, font_h = font.size(c)
|
|
|
|
window = pg.display.set_mode((font_w, font_h))
|
|
|
|
rendered_char = font.render(c, False, (255, 255, 255))
|
|
|
|
window.blit(rendered_char, (0, 0))
|
|
|
|
pg.display.update()
|
|
pg.image.save(window, 'temp.png')
|
|
|
|
pg.quit()
|
|
|
|
image = Image.open('temp.png')
|
|
|
|
w, h = image.size
|
|
|
|
white = 0
|
|
for pixel_y in range(h):
|
|
for pixel_x in range(w):
|
|
pixel = image.getpixel((pixel_x, pixel_y))
|
|
|
|
if pixel == (255, 255, 255):
|
|
white += 1
|
|
|
|
sorted_chars[c] = white/(w * h)
|
|
|
|
sorted_chars = dict(sorted(sorted_chars.items(), key=lambda item: item[1]))
|
|
|
|
chr_str = ''
|
|
for k, v in sorted_chars.items():
|
|
chr_str += k
|
|
|
|
print(chr_str)
|
|
|
|
|