Python is a powerful programming language that can be used for a wide range of applications. One such application is creating and manipulating images. In this tutorial, we will explore how to use the Python Pillow library to create and modify images programmatically. We will cover how to create images of different sizes, colors, and formats, and how to add text and other elements to them. We will also discuss the various functionalities provided by the Pillow library, such as image processing and filtering. By the end of this tutorial, you will have a good understanding of how to create and manipulate images using Python and Pillow.
from PIL import ImageFont, Image, ImageDraw
import os
# Get the path to the font file
# font_path = os.path.join(os.getcwd(), "arial.ttf")
font_path = "/Library/Fonts/Arial.ttf"
# Create a new image with size 1280x720 pixels and a white background
image = Image.new('RGB', (1280, 720), (255, 255, 255))
# Create a drawing object
draw = ImageDraw.Draw(image)
# Define the font size and font family to use for the text
font = ImageFont.truetype(font_path, 64)
# Define the text to write on the image
text = "My YouTube Thumbnail"
# get the bounding box of the text
text_bbox = draw.textbbox((0, 0), text, font=font)
# calculate the width and height of the bounding box
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# calculate the position of the text
x = (image.width - text_width) / 2
y = (image.height - text_height) / 2
# draw the text on the image
draw.text((x, y), text, font=font, fill=(0, 0, 0))
# Save the image as a JPEG file
image.save('thumbnail.jpg')
output