See also: Exploring PyQt5: Building Interactive GUIs with Python
Let’s put text on images with Python
pip install Pillow requests
This will install the Pillow and requests libraries. Pillow is an image processing library that provides support for the image format used in the code, while requests is used to make HTTP requests and download the image from a URL. Make sure you have Python and pip installed in your environment before running these commands.
Code
from PIL import Image, ImageDraw, ImageFont import requests from io import BytesIO # Image URL image_url = "https://source.unsplash.com/random/800x400?nature" # Download the image from the URL response = requests.get(image_url) img = Image.open(BytesIO(response.content)) # Create an ImageDraw object draw = ImageDraw.Draw(img) # Specify the font and size font_size = 30 font = ImageFont.truetype("arial.ttf", font_size) # Replace "arial.ttf" with the path to your font # Coordinates and text to be written x, y = 10, 10 text = "Hello, Python!" # Add text to the image draw.text((x, y), text, font=font, fill=(0, 0, 0)) # Save the modified image img.save("modified_image.jpg")
Conclusion
Venturing into the intersection of programming and image processing grants you full control over the visual aesthetics of your images. The ability to automate tasks such as adding text to an image not only saves time but also sparks creativity across various applications, ranging from crafting personalized memes to generating dynamic visual content.
We trust that this tutorial has provided valuable insights to kickstart your own explorations in the realm of image manipulation with Python. Keep experimenting and uncover the myriad possibilities that the fusion of code and visualization has to offer.