Image Drawing with Circle and Rectangle

  • Share this:

Code introduction


This function first loads an image, then creates a drawing context, draws a red outline circle and a blue rectangle on the image, and finally saves the modified image to the specified output path.


Technology Stack : Pillow library

Code Type : Pillow library image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageDraw

def draw_circle_and_rectangle(image_path, output_path):
    # Load the image
    img = Image.open(image_path)
    
    # Create a drawing context
    draw = ImageDraw.Draw(img)
    
    # Draw a circle and a rectangle on the image
    draw.ellipse((100, 100, 200, 200), outline='red')
    draw.rectangle((50, 50, 150, 150), fill='blue')
    
    # Save the image
    img.save(output_path)                
              
Tags: