chore: Refactor imageToAsciiText and asciiTextToImage functions. it works now
This commit is contained in:
parent
0ed934d08f
commit
135fd6c311
1 changed files with 16 additions and 10 deletions
26
main.py
26
main.py
|
@ -23,25 +23,29 @@ def imageToAsciiText(path, cell_size=(8, 8)):
|
|||
print(result)
|
||||
return result
|
||||
|
||||
def asciiTextToImage(asciiText, fontPath, fontSize):
|
||||
def asciiTextToImage(asciiText, fontPath, cellSize, fontSize, clarityFactor):
|
||||
# Split the ASCII art into lines
|
||||
lines = asciiText.split('\n')
|
||||
|
||||
# Calculate the image size needed
|
||||
font = ImageFont.truetype(os.path.join(currentDir, fontPath), fontSize)
|
||||
font = ImageFont.truetype(os.path.join(currentDir, fontPath), fontSize * clarityFactor)
|
||||
max_width = max([len(line) for line in lines])
|
||||
image_width = max_width * fontSize
|
||||
image_height = len(lines) * fontSize
|
||||
|
||||
image_width = max_width * cellSize * clarityFactor
|
||||
image_height = len(lines) * cellSize * clarityFactor
|
||||
|
||||
# Create a new blank image
|
||||
image = Image.new('RGB', (image_width, image_height), 'black')
|
||||
image = Image.new('RGB', (image_width, image_height), 'white')
|
||||
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
|
||||
x = 0
|
||||
for char in line:
|
||||
draw.text((x, y), char, fill='black', font=font, spacing=0)
|
||||
x += cellSize * clarityFactor
|
||||
y += cellSize * clarityFactor
|
||||
|
||||
return image
|
||||
|
||||
|
@ -56,8 +60,10 @@ 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
|
||||
fontPath = 'fonts/courbd.ttf' # Specify the path to your font file
|
||||
fontSize = 10 # font size in points
|
||||
clarityFactor = 8
|
||||
|
||||
|
||||
# Process all images in the current directory
|
||||
for filename in os.listdir(currentDir):
|
||||
|
@ -71,7 +77,7 @@ for filename in os.listdir(currentDir):
|
|||
|
||||
# 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 = asciiTextToImage(asciiText, fontPath, int(cellSize[0]), fontSize, clarityFactor)
|
||||
|
||||
asciiImage.save(outputPath)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue