Python Draw Rectangle On Image: An Introduction
Python is an open source, high-level programming language that is widely used for web and software development. It is an object-oriented language that is simple to learn and use. Python is also a great language for data analysis, image processing and graphical plotting. In this tutorial, we will be learning how to draw a rectangle on an image using Python. We will be using the Pillow library, which is a fork of the Python Imaging Library (PIL).
The Pillow library is a powerful library that provides the tools needed to manipulate images. It can be used to create and modify images in a variety of formats. It also provides support for drawing shapes and text on images. In this tutorial, we will be using the ImageDraw module, which provides the tools to create and draw shapes on an image.
Creating an Image with Pillow
Before we can draw a rectangle on an image, we first need to create an image. This can be done using the Image module from the Pillow library. The Image module provides the tools needed to create and save images in a variety of formats. We can use the new() method to create a new image. The new() method takes two arguments, the width and height of the image. For example, to create a new image that is 500×500 pixels, we can use the following code:
import Image
img = Image.new(‘RGB’, (500,500))
The new() method creates a new image object that is 500×500 pixels. We can then use the save() method to save the image to a file. The save() method takes two arguments, the filename and the image format. For example, to save the image as a PNG file, we can use the following code:
img.save(‘myimage.png’, ‘PNG’)
Drawing a Rectangle on an Image
Now that we have created an image, we can use the ImageDraw module to draw a rectangle on it. The ImageDraw module provides the tools to draw shapes and text on an image. To draw a rectangle on an image, we need to use the rectangle() method. The rectangle() method takes four arguments, the coordinates of the top-left and bottom-right corners of the rectangle. For example, to draw a rectangle starting at (10,10) and ending at (100,100), we can use the following code:
import Image, ImageDraw
# create an image
img = Image.new(‘RGB’, (500,500))
# create a draw object
draw = ImageDraw.Draw(img)
# draw a rectangle
draw.rectangle((10, 10, 100, 100))
The rectangle() method will draw a rectangle on the image starting at (10,10) and ending at (100,100). We can then use the save() method to save the image to a file.
Drawing a Filled Rectangle on an Image
The ImageDraw module also provides the ability to draw a filled rectangle on an image. To draw a filled rectangle, we need to use the rectangle() method. The rectangle() method takes six arguments, the coordinates of the top-left and bottom-right corners of the rectangle and the fill color. For example, to draw a filled rectangle with a red fill color, we can use the following code:
import Image, ImageDraw
# create an image
img = Image.new(‘RGB’, (500,500))
# create a draw object
draw = ImageDraw.Draw(img)
# draw a filled rectangle
draw.rectangle((10, 10, 100, 100), fill=(255,0,0))
The rectangle() method will draw a filled rectangle on the image starting at (10,10) and ending at (100,100). The fill color is specified using the fill argument. In this example, we have used a red fill color (RGB value of 255,0,0). We can then use the save() method to save the image to a file.
Conclusion
In this tutorial, we have learned how to draw a rectangle on an image using Python and the Pillow library. We have seen how to create an image and draw a rectangle on it. We have also seen how to draw a filled rectangle on an image. We hope that you have found this tutorial useful and have been able to learn something new.

Leave a Reply