From 135fd6c311be97967ccfe3041c8fbd066ff4be4f Mon Sep 17 00:00:00 2001 From: Achille Date: Thu, 4 Jul 2024 22:09:44 +0200 Subject: [PATCH] chore: Refactor imageToAsciiText and asciiTextToImage functions. it works now --- main.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index 1ea621f..89ad46f 100644 --- a/main.py +++ b/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)