Rotate and Draw Circle on Image

  • Share this:

Code introduction


This function reads an image file, rotates it by a specified angle, draws a circle on the rotated image, and then saves the modified image to a specified path.


Technology Stack : Pillow

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
from PIL import Image, ImageFilter, ImageDraw

def rotate_and_draw_circle(image_path, output_path, angle=45):
    """
    Rotate an image and draw a circle on the rotated image.
    """
    with Image.open(image_path) as img:
        # Rotate the image
        rotated_img = img.rotate(angle, expand=True)
        
        # Create a drawing context
        draw = ImageDraw.Draw(rotated_img)
        
        # Draw a circle on the rotated image
        center = (rotated_img.width // 2, rotated_img.height // 2)
        radius = min(rotated_img.width, rotated_img.height) // 4
        draw.ellipse([center[0] - radius, center[1] - radius, center[0] + radius, center[1] + radius])
        
        # Save the modified image
        rotated_img.save(output_path)                
              
Tags: