Image Rotation and Contrast Enhancement

  • Share this:

Code introduction


This function takes an image path and an angle of rotation as parameters, loads the image, rotates it by the specified angle, enhances its contrast, and finally returns the processed image.


Technology Stack : Pillow (PIL)

Code Type : Image processing function

Code Difficulty : Intermediate


                
                    
def random_image_rotation(image_path, angle):
    from PIL import Image, ImageEnhance
    
    # Load the image
    img = Image.open(image_path)
    
    # Rotate the image
    rotated_img = img.rotate(angle, expand=True)
    
    # Enhance the contrast of the rotated image
    enhancer = ImageEnhance.Contrast(rotated_img)
    enhanced_img = enhancer.enhance(1.5)
    
    return enhanced_img                
              
Tags: