Draw Circle on Image

  • Share this:

Code introduction


This function opens a specified image file and draws a circle on the image. Users can specify the center point, radius, and color of the circle.


Technology Stack : Pillow library

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageDraw

def draw_circle(img_path, center, radius, color):
    # Open an image file
    with Image.open(img_path) as img:
        # Create a drawing context
        draw = ImageDraw.Draw(img)
        # Draw a circle at the given center and radius with the specified color
        draw.ellipse([center[0]-radius, center[1]-radius, center[0]+radius, center[1]+radius], fill=color)
        # Save the image with the drawn circle
        img.save(img_path)                
              
Tags: