from PIL import Image, ImageDraw, ImageFont
import os
# Define the input sentence
input_sentence = "My name is jitender singh, and I am a Good Guy."
# Split the sentence into words
words = input_sentence.split()
# Initialize a font and image size
font = ImageFont.truetype("/Users/jitendersingh/Library/Fonts/Tabloid Scuzzball.otf", 30)
image_size = (800, 200)
# Create a directory to store the images
os.makedirs("/Users/jitendersingh/Desktop/word_images", exist_ok=True)
# Find the maximum text height among all words
max_text_height = max([font.getsize(w)[1] for w in words])
# Initialize the starting position for text
x_position = 10
y_position = (image_size[1] - max_text_height) // 2 # Center vertically
# Iterate through each word and generate images
for i, word in enumerate(words):
# Create a new image
image = Image.new("RGB", image_size)
draw = ImageDraw.Draw(image)
# Initialize a list to store the words with colors
colored_words = []
# Loop through the words to set the current word to yellow and others to white
for j, w in enumerate(words):
if j == i:
# Color the current word (word at index i) in yellow
draw.text((x_position, y_position), w, font=font, fill="yellow")
else:
# Keep other words in white
draw.text((x_position, y_position), w, font=font, fill="white")
# Update the x_position to create spacing between words
x_position += font.getsize(w)[0] + 10 # Add 10 pixels spacing between words
# Reset the x_position for the next line
x_position = 10
# Save the image
image.save(f"/Users/jitendersingh/Desktop/word_images/word_image_{i}.png")
0 comments:
Post a Comment