Drawing Shapes and Text on Images with Pillow

Drawing Shapes and Text on Images with Pillow

To get started with drawing shapes and text on images using Python, we first need to install the Pillow library. Pillow is a fork of the Python Imaging Library (PIL) and provides easy-to-use functions for opening, manipulating, and saving many different image file formats.

To install Pillow, we can use pip, which is the package installer for Python. Simply run the following command in your terminal or command prompt:

pip install Pillow

After installing Pillow, we need to import the required modules for our project. The two main modules we’ll be using are Image and ImageDraw from the Pillow library. The Image module provides a class with the same name which is used to represent a PIL image, while the ImageDraw module provides simple 2D graphics for Image objects.

Here’s an example of how to import these modules:

from PIL import Image, ImageDraw

Now that we have Pillow installed and the necessary modules imported, we’re ready to move on to drawing shapes on images.

Drawing Shapes on Images

To draw shapes on an image, we first need to create an instance of the ImageDraw class by passing in the image object that we want to draw on. We can then use the various drawing methods provided by the ImageDraw class to create shapes such as lines, rectangles, circles, and more.

For example, to draw a simple rectangle on an image, we would use the following code:

# Load an image
image = Image.open('example.jpg')

# Create an ImageDraw object
draw = ImageDraw.Draw(image)

# Define the coordinates of the rectangle's top-left and bottom-right corners
top_left = (50, 50)
bottom_right = (150, 150)

# Draw the rectangle on the image
draw.rectangle([top_left, bottom_right], outline='red', width=3)

# Save the modified image
image.save('example_with_rectangle.jpg')

This code will draw a red rectangle with a width of 3 pixels on the image, starting at the coordinates (50, 50) and ending at (150, 150).

Similarly, to draw a circle, we can use the ellipse method:

# Define the bounding box for the circle
bounding_box = [70, 70, 130, 130]

# Draw the circle on the image
draw.ellipse(bounding_box, outline='blue', width=5)

The bounding_box defines the top-left and bottom-right corners of a rectangle that the circle will be inscribed within. In this case, the circle will be drawn with a blue outline and a width of 5 pixels.

Other shapes can be drawn using similar methods. Here are a few more examples:

  • To draw a line, use draw.line([(x1, y1), (x2, y2)], fill='color', width=n)
  • To draw an ellipse, use draw.ellipse([(x1, y1), (x2, y2)], outline='color', width=n)
  • To draw a polygon, use draw.polygon([(x1, y1), (x2, y2), ...], outline='color', fill='color')

With these simple methods, you can create complex drawings by combining multiple shapes and colors. Just remember to always save the modified image using the save method of the Image object.

Adding Text to Images

Adding text to images using the Pillow library is just as straightforward as drawing shapes. To add text, we use the text method of the ImageDraw class. This method takes the coordinates of where the text should begin, the text string itself, and optionally, the font and fill color.

First, we need to choose a font for our text. Pillow supports TrueType and OpenType fonts, which can be loaded using the ImageFont module. Here’s how to load a font:

from PIL import ImageFont

# Load a font
font = ImageFont.truetype('arial.ttf', size=15)

Now let’s add some text to an image:

# Load an image
image = Image.open('example.jpg')

# Create an ImageDraw object
draw = ImageDraw.Draw(image)

# Specify the text and font
text = "Hello, World!"
font = ImageFont.truetype('arial.ttf', size=15)

# Define the position for the text
position = (100, 100)

# Add text to the image
draw.text(position, text, font=font, fill='green')

# Save the modified image
image.save('example_with_text.jpg')

This code will add the text “Hello, World!” to the image in green, using a 15-point Arial font, starting at the coordinates (100, 100).

Just like with shapes, you can combine multiple text calls with different positions, fonts, and colors to create more complex designs.

Here are a few more examples of how to customize the text:

  • draw.text((x, y), "text", font=font, fill='color')
  • draw.text((x, y), "text", font=font, fill='color', anchor='anchor')
  • To add a shadow effect, draw the text twice with a slight offset and different colors.

With these techniques, you can easily add labels, captions, or any other textual information to your images using Python and Pillow.

Saving and Displaying Modified Images

Once you have modified your image by drawing shapes or adding text, you will want to save the changes and possibly display the image. The Pillow library makes this process simple with the save and show methods of the Image object.

To save the modified image to a file, simply call the save method with the desired file name and format. Here’s an example:

# Save the modified image to a file
image.save('modified_image.png')

You can specify the format of the image by providing the file extension (such as .png, .jpg, .bmp, etc.) in the file name. If you want to explicitly set the format, you can pass it as a second argument:

# Save the image in JPEG format
image.save('modified_image.jpg', 'JPEG')

If you want to quickly display the modified image without saving it, you can use the show method. This will open the image using the default image viewer on your system:

# Display the modified image
image.show()

It is important to note that the show method is mainly intended for debugging purposes and may not work the same way on all operating systems. For a more reliable way to display images in a specific context (such as a web application), you might need to use different methods or libraries.

Here are a few more tips for saving and displaying modified images:

  • To save images with transparency (like PNGs with alpha channels), make sure to use a format that supports transparency.
  • If you are working with large images or need to save multiple images, ponder using a loop or a function to automate the process.
  • When displaying images in a web application, you may need to save the modified image to a temporary file or buffer and then serve it to the client.

With the ability to save and display your modified images, you can now fully utilize the Pillow library to enhance and personalize your visual content using Python.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *