first commit
This commit is contained in:
commit
38bef90d39
6 changed files with 136 additions and 0 deletions
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}")
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue