How to change the color of text added to an image with Pillow?
Oct 13, 2025
Leave a message
In the realm of digital image processing, the ability to manipulate text on images is a valuable skill that can enhance visual appeal and convey information effectively. Pillow, a powerful Python library for image processing, offers a straightforward way to change the color of text added to an image. As a Pillow supplier, I'm excited to share with you a detailed guide on how to achieve this, along with some practical tips and examples.
Prerequisites
Before we dive into the process of changing text color on an image using Pillow, you'll need to have a few things in place:
- Python Installation: Ensure that Python is installed on your system. You can download the latest version of Python from the official website (python.org).
- Pillow Library: Install the Pillow library using
pip. Open your terminal or command prompt and run the following command:
pip install pillow
- Basic Python Knowledge: Familiarity with Python programming concepts such as variables, functions, and file handling will be beneficial.
Step-by-Step Guide
Let's break down the process of changing the color of text added to an image using Pillow into a series of steps:
Step 1: Import the Necessary Libraries
First, you need to import the Image, ImageDraw, and ImageFont modules from the Pillow library. These modules provide the functionality required to open, draw on, and add text to images.
from PIL import Image, ImageDraw, ImageFont
Step 2: Open the Image
Next, open the image to which you want to add text. You can use the Image.open() method to load an image file from your local system.
# Open the image
image = Image.open('your_image.jpg')
Replace 'your_image.jpg' with the actual path to your image file.
Step 3: Create a Drawing Context
To draw text on the image, you need to create a drawing context using the ImageDraw.Draw() method. This context allows you to perform various drawing operations on the image.
# Create a drawing context
draw = ImageDraw.Draw(image)
Step 4: Choose a Font and Size
Select a font and size for the text you want to add. You can use the ImageFont.truetype() method to load a TrueType font file from your system.
# Choose a font and size
font = ImageFont.truetype('arial.ttf', 36)
Replace 'arial.ttf' with the path to the font file you want to use, and 36 with the desired font size.
Step 5: Define the Text and Its Position
Specify the text you want to add to the image and its position. You can use a tuple to define the coordinates of the top-left corner of the text.
# Define the text and its position
text = "Hello, World!"
position = (50, 50)
Step 6: Choose the Text Color
Now, it's time to choose the color of the text. You can represent colors using RGB tuples, where each value ranges from 0 to 255. For example, (255, 0, 0) represents red, (0, 255, 0) represents green, and (0, 0, 255) represents blue.
# Choose the text color
text_color = (255, 0, 0) # Red
Step 7: Draw the Text on the Image
Finally, use the draw.text() method to draw the text on the image with the specified color.


# Draw the text on the image
draw.text(position, text, font=font, fill=text_color)
Step 8: Save the Modified Image
After adding the text to the image, save the modified image using the image.save() method.
# Save the modified image
image.save('modified_image.jpg')
Replace 'modified_image.jpg' with the desired name and path for the output image file.
Example Code
Here's the complete example code that combines all the steps described above:
from PIL import Image, ImageDraw, ImageFont
# Open the image
image = Image.open('your_image.jpg')
# Create a drawing context
draw = ImageDraw.Draw(image)
# Choose a font and size
font = ImageFont.truetype('arial.ttf', 36)
# Define the text and its position
text = "Hello, World!"
position = (50, 50)
# Choose the text color
text_color = (255, 0, 0) # Red
# Draw the text on the image
draw.text(position, text, font=font, fill=text_color)
# Save the modified image
image.save('modified_image.jpg')
Practical Tips
- Color Selection: Experiment with different color combinations to find the one that best suits your image and message. You can use online color pickers to get inspiration and select the perfect colors.
- Font Selection: Choose a font that is easy to read and complements the style of your image. You can download free fonts from websites like Google Fonts and use them in your projects.
- Positioning: Pay attention to the position of the text on the image to ensure that it doesn't overlap with important elements or distract from the overall composition. You can use trial and error to find the optimal position.
- Antialiasing: By default, Pillow uses antialiasing to smooth the edges of the text, making it look more professional. However, you can disable antialiasing by setting the
antialiasparameter toFalsein thedraw.text()method if you prefer a more pixelated look.
Conclusion
Changing the color of text added to an image using Pillow is a simple yet powerful technique that can enhance the visual impact of your images. By following the steps outlined in this guide and applying the practical tips, you'll be able to create stunning images with custom text in no time.
As a Pillow supplier, we are committed to providing high-quality products and excellent customer service. If you're interested in purchasing our Pillow products, such as Household Pillow or Memory Foam Pillow, please feel free to contact us for more information and to discuss your procurement needs. We look forward to working with you!
References
- Pillow Documentation: https://pillow.readthedocs.io/
- Python Official Website: https://www.python.org/
- Google Fonts: https://fonts.google.com/
