first commit
This commit is contained in:
commit
38bef90d39
6 changed files with 136 additions and 0 deletions
BIN
cuantas-calorias-tiene-un-tomate.jpg
Normal file
BIN
cuantas-calorias-tiene-un-tomate.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 70 KiB |
BIN
fonts/couri.ttf
Normal file
BIN
fonts/couri.ttf
Normal file
Binary file not shown.
4
imageGen.py
Normal file
4
imageGen.py
Normal file
|
@ -0,0 +1,4 @@
|
|||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
|
||||
|
81
main.py
Normal file
81
main.py
Normal file
|
@ -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}")
|
||||
|
||||
|
BIN
output/cuantas-calorias-tiene-un-tomate_asciiart.jpg
Normal file
BIN
output/cuantas-calorias-tiene-un-tomate_asciiart.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 121 KiB |
51
temp.py
Normal file
51
temp.py
Normal file
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue