import numpy as np
from PIL import Image
# Liste de caractères à utiliser pour l'ASCII art
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
def scale_image(image, new_width=100):
"""
Redimensionne l'image tout en maintenant le ratio de l'image.
"""
width, height = [Link]
ratio = height / width
new_height = int(new_width * ratio)
resized_image = [Link]((new_width, new_height))
return resized_image
def grayscale_image(image):
"""
Convertit l'image en niveaux de gris.
"""
return [Link]("L")
def map_pixels_to_ascii(image):
"""
Mappe chaque pixel de l'image à un caractère ASCII correspondant à son
intensité.
"""
pixels = [Link](image)
ascii_str = ""
for pixel in [Link]():
ascii_str += ASCII_CHARS[pixel // 25] # Choisit un caractère basé sur la
luminosité
return ascii_str
def image_to_ascii(image_path, new_width=100):
"""
Convertit l'image en ASCII art.
"""
try:
image = [Link](image_path)
except Exception as e:
print(f"Erreur lors de l'ouverture de l'image : {e}")
return
image = scale_image(image, new_width)
image = grayscale_image(image)
ascii_str = map_pixels_to_ascii(image)
ascii_width = [Link]
ascii_str_len = len(ascii_str)
ascii_image = ""
for i in range(0, ascii_str_len, ascii_width):
ascii_image += ascii_str[i:i+ascii_width] + "\n"
return ascii_image
# Exemple d'utilisation
image_path = "votre_image.jpg" # Remplacez par le chemin de votre image
ascii_art = image_to_ascii(image_path)
print(ascii_art)