Last active
January 8, 2020 17:23
-
-
Save aguhcel/353dc64871a658ba6f566ca35aa4e6d9 to your computer and use it in GitHub Desktop.
Conversion de Imagen a ASCII
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importamos las herramientas para el procesamiento de la imagen | |
from PIL import Image | |
# Escogemos la imagen que vamos a convertir | |
# Hay que aclarar que el 10 es por que se reescala una imagen de 1/10 | |
# Todo dependera de la imagen que ocupemos es el tamaño que vamos a reescalar | |
# En mi caso es una imagen de 512*512 que se reescala a una imagen de 51*51 | |
imagen = Image.open("doge.jpg") | |
imagen = imagen.resize((int(imagen.width / 10), int(imagen.height / 10))) | |
# Definimos los caracteres ASCII que ocuparemos | |
caracteres = '.', '*', '$' | |
texto = '' | |
a, b = 0, 0 | |
x, y, z = 0, 0, 0 | |
# Para cada pixel en la imagen | |
while y < imagen.height: | |
# Encontramos el brillo de la imagen | |
brillo = sum(imagen.getpixel((x, y))) / 3 | |
# dependiendo del brillo es ASCII que se le asigna | |
while z < 3: | |
a += 85 | |
if brillo <= a and brillo >= b: | |
texto += caracteres[z] | |
texto += caracteres[z] | |
b += 85 | |
z += 1 | |
z = 0 | |
a = 0 | |
b = 0 | |
if x == imagen.width - 1: | |
texto += '\n' | |
y += 1 | |
x = 0 | |
else: | |
x += 1 | |
#Print the output | |
print(texto) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment