commit 38bef90d3925b8bcefe0867e42565f3bb3b9e2ce Author: gribse Date: Thu Jul 4 18:34:48 2024 +0200 first commit diff --git a/cuantas-calorias-tiene-un-tomate.jpg b/cuantas-calorias-tiene-un-tomate.jpg new file mode 100644 index 0000000..364d7ba Binary files /dev/null and b/cuantas-calorias-tiene-un-tomate.jpg differ diff --git a/fonts/couri.ttf b/fonts/couri.ttf new file mode 100644 index 0000000..22b73d4 Binary files /dev/null and b/fonts/couri.ttf differ diff --git a/imageGen.py b/imageGen.py new file mode 100644 index 0000000..8d23470 --- /dev/null +++ b/imageGen.py @@ -0,0 +1,4 @@ +from PIL import Image, ImageDraw, ImageFont + + + diff --git a/main.py b/main.py new file mode 100644 index 0000000..1262a20 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +from PIL import Image, ImageDraw, ImageFont +import numpy as np +import os + +def imageToAsciiText(path, cell_size=(8, 8)): + image = Image.open(path) + image = image.convert('L') # Convert to grayscale + width, height = image.size + pixels = np.array(image) + ascii_characters = "@%#*+=-:. " # From darkest to lightest + result = "" + + for i in range(0, height, cell_size[1]): + for j in range(0, width, cell_size[0]): + cell = pixels[i:i+cell_size[1], j:j+cell_size[0]] + if cell.size == 0: + continue + average_brightness = np.mean(cell) + character_index = int(average_brightness * (len(ascii_characters) - 1) / 255) + result += ascii_characters[character_index] + result += '\n' + + print(result) + return result + +def asciiTextToImage(asciiText, outputPath, fontPath, fontSize): + # Split the ASCII art into lines + lines = asciiText.split('\n') + + # Calculate the image size needed + font = ImageFont.truetype(os.path.join(currentDir, fontPath), fontSize) + max_width = max([len(line) for line in lines]) + image_width = max_width * fontSize + image_height = len(lines) * fontSize + + # Create a new blank image + image = Image.new('RGB', (image_width, image_height), 'black') + draw = ImageDraw.Draw(image) + + # Draw each line of ASCII art + y = 0 + for line in lines: + draw.text((0, y), line, fill='white', font=font, spacing=0) + y += fontSize + + return image + + +# Example usage +# Get the current directory +currentDir = os.path.dirname(os.path.abspath(__file__)) + +# Create the export directory if it doesn't exist +exportDir = os.path.join(currentDir, "output") +os.makedirs(exportDir, exist_ok=True) + +# Edit params +cellSize = (8, 8) +fontPath = 'fonts/couri.ttf' # Specify the path to your font file +fontSize = 12 # Adjust font size as needed + +# Process all images in the current directory +for filename in os.listdir(currentDir): + if filename.endswith(".jpg") or filename.endswith(".png"): + # Construct the full path of the image + imagePath = os.path.join(currentDir, filename) + + # Generate the output file name + outputFilename = os.path.splitext(filename)[0] + "_asciiart.jpg" + outputPath = os.path.join(exportDir, outputFilename) + + # Call the imageToAsciiText function and save the result to the output file + asciiText = imageToAsciiText(imagePath, cellSize) + asciiImage = asciiTextToImage(asciiText, outputPath, fontPath, int(cellSize[0])) + + asciiImage.save(outputPath) + + # Print the output file path + print(f"Processed image: {outputPath}") + + diff --git a/output/cuantas-calorias-tiene-un-tomate_asciiart.jpg b/output/cuantas-calorias-tiene-un-tomate_asciiart.jpg new file mode 100644 index 0000000..1628b08 Binary files /dev/null and b/output/cuantas-calorias-tiene-un-tomate_asciiart.jpg differ diff --git a/temp.py b/temp.py new file mode 100644 index 0000000..1dbb3c3 --- /dev/null +++ b/temp.py @@ -0,0 +1,51 @@ +from PIL import Image, ImageDraw, ImageFont +import numpy as np + +def generer_images_ascii(taille_cellule, font_path="arial.ttf", font_size=14): + caracteres_ascii = "@%#*+=-:. " + images_ascii = {} + font = ImageFont.truetype(font_path, font_size) + + for caractere in caracteres_ascii: + image = Image.new('L', taille_cellule, "black") + draw = ImageDraw.Draw(image) + w, h = draw.textsize(caractere, font=font) + draw.text(((taille_cellule[0]-w)/2, (taille_cellule[1]-h)/2), caractere, fill="white", font=font) + images_ascii[caractere] = np.array(image) + + return images_ascii + +def comparer_cellule_avec_ascii(cellule, images_ascii): + scores = {} + for caractere, image_ascii in images_ascii.items(): + difference = np.abs(cellule - image_ascii) + score = np.mean(difference) + scores[caractere] = score + return min(scores, key=scores.get) + +def charger_image_et_convertir_ascii(chemin, taille_cellule=(8, 8), font_path="arial.ttf", font_size=14): + image = Image.open(chemin) + image = image.convert('L') + largeur, hauteur = image.size + pixels = np.array(image) + images_ascii = generer_images_ascii(taille_cellule, font_path, font_size) + resultat = "" + + for i in range(0, hauteur, taille_cellule[1]): + for j in range(0, largeur, taille_cellule[0]): + cellule = pixels[i:i+taille_cellule[1], j:j+taille_cellule[0]] + if cellule.shape[0] != taille_cellule[1] or cellule.shape[1] != taille_cellule[0]: + continue + caractere = comparer_cellule_avec_ascii(cellule, images_ascii) + resultat += caractere + resultat += '\n' + + return resultat + +# Exemple d'utilisation +chemin_image = "chemin/vers/votre/image.jpg" +taille_cellule = (8, 8) # Ajustez selon les besoins +font_path = "arial.ttf" # Ajustez le chemin vers la police si nécessaire +font_size = 14 # Ajustez la taille de la police si nécessaire +resultat_ascii = charger_image_et_convertir_ascii(chemin_image, taille_cellule, font_path, font_size) +print(resultat_ascii) \ No newline at end of file