# language: Python, file: ascii_art.py from PIL import Image # Строка символов от тёмного к светлому CHARS = "@%#*+=-:. " def image_to_ascii(image_path, width=100): img = Image.open(image_path) # Сохраняем пропорции (символы выше, чем шире, поэтому делим высоту на 2) aspect = img.height / img.width height = int(aspect * width * 0.5) img = img.resize((width, height)) img = img.convert("L") # в оттенки серого pixels = img.getdata() ascii_str = "" for i, pixel in enumerate(pixels): # pixel от 0 (чёрный) до 255 (белый) # индекс в строке CHARS: 0 для чёрного, 9 для белого index = int(pixel / 255 * (len(CHARS) - 1)) ascii_str += CHARS[index] if (i + 1) % width == 0: ascii_str += "\n" return ascii_str print(image_to_ascii("femboy.jpg", width=80))