Rotate and Save Image with Pillow

  • Share this:

Code introduction


This function takes an image path and an angle as parameters, uses the Pillow library's Image and ImageOps modules to rotate the image by the specified angle, and saves the rotated image in the current directory.


Technology Stack : Pillow library (Python Imaging Library)

Code Type : Image processing

Code Difficulty : Intermediate


                
                    
def rotate_image(image_path, angle):
    from PIL import Image, ImageOps

    # Open the image file
    with Image.open(image_path) as img:
        # Rotate the image
        rotated_img = ImageOps.rotate(img, angle)
        # Save the rotated image
        rotated_img.save("rotated_" + image_path)