Demo image generator for python

Generates crisp images
This commit is contained in:
Nic Limper
2023-05-27 21:46:51 +02:00
parent 6380cd15cb
commit 3e4175a790

View File

@@ -0,0 +1,43 @@
from PIL import Image, ImageDraw, ImageFont
# Create a new paletted image with indexed colors
image = Image.new('P', (296, 128))
# Define the color palette (white, black, red)
palette = [
255, 255, 255, # white
0, 0, 0, # black
255, 0, 0 # red
]
# Assign the color palette to the image
image.putpalette(palette)
# Initialize the drawing context
draw = ImageDraw.Draw(image)
# Define the text lines
line1 = 'OpenEPaperLink'
line2 = 'Demo image'
# Define the fonts and sizes
font_line1 = ImageFont.truetype('arial.ttf', size=36) # Change the font file and size as per your preference
font_line2 = ImageFont.truetype('arial.ttf', size=16) # Change the font file and size as per your preference
# Calculate the text bounding boxes to get the text widths and heights
text_bbox_line1 = draw.textbbox((0, 0), line1, font=font_line1)
text_bbox_line2 = draw.textbbox((0, 0), line2, font=font_line2)
# Calculate the text positions to center the lines horizontally
text_position_line1 = ((image.width - (text_bbox_line1[2] - text_bbox_line1[0])) // 2, 20)
text_position_line2 = ((image.width - (text_bbox_line2[2] - text_bbox_line2[0])) // 2, 80)
# Write the text on the image
draw.text(text_position_line1, line1, fill=2, font=font_line1) # Use palette index 1 for black color
draw.text(text_position_line2, line2, fill=1, font=font_line2) # Use palette index 2 for red color
# Convert the image to 24-bit RGB
rgb_image = image.convert('RGB')
# Save the image as JPEG with maximum quality
rgb_image.save('output.jpg', 'JPEG', quality="maximum")