SoFunction
Updated on 2025-04-11

Python image processing complete code to add rounded corner effect to images

Code parsing

First, we need to import theImageandImageDrawModule. These two modules provide rich image processing capabilities.

from PIL import Image, ImageDraw

Next, we define a name calledround_cornersThe function of , which accepts three parameters:image_path(Input the path to the picture),output_path(Path to output the image) andradius(Radius of rounded corners).

def round_corners(image_path, output_path, radius):

Inside the function, we first useMethod opens the image and converts it to "RGBA" mode for transparent processing.

    image = (image_path).convert("RGBA")

Then, we create a blank image of the same size as the original image as the mask, which will be used to achieve the rounded corner effect.

    mask = ("RGBA", , (0, 0, 0, 0))

Next, we useCreate a brush object and draw a rounded rectangle on the mask. The fill color of the rounded rectangle is set to white (RGB value is (255, 255, 255)).

    draw = (mask)
    draw.rounded_rectangle((0, 0, , ), radius, fill=(255, 255, 255))

Now, we create a new image objectresult, its size is the same as the original image and is set to "RGBA" mode.

    result = ("RGBA", )

usepasteMethod, we paste the original image onto the new image and use the mask we created earlier to achieve the rounded corner effect.

    (image, mask=mask)

Finally, we usesaveMethod saves the processed image to the specified output path.

    (output_path)

Example of usage

Here is a useround_cornersExample of function. Let's assume there is a picture calledand want to set its rounded corner radius to 300 pixels, save the processed picture as20250306_output.png

source_img = r'D:\test_pic\'
output_img = r'D:\test_pic\20250306_output.png'
round_corners(source_img, output_img, 300)

Complete example

from PIL import Image, ImageDraw

def round_corners(image_path, output_path, radius):
    # Open the image and convert it to RGBA mode to support transparent backgrounds    image = (image_path).convert("RGBA")
    
    # Create a blank image of the same size as the original image as the mask    mask = ("RGBA", , (0, 0, 0, 0))
    
    # Create a brush object to draw a graphic on a mask    draw = (mask)
    
    # Draw a rounded rectangle on the mask, the rounded radius is radius, and the fill color is white ((255, 255, 255, 255) in RGBA mode)    draw.rounded_rectangle((0, 0, , ), radius, fill=(255, 255, 255, 255))
    
    # Create a new image object with the same size as the original image and the mode is RGBA    result = ("RGBA", )
    
    # Paste the original image onto the new image, use a mask to mask it, and only the part inside the rounded rectangle is retained    (image, mask=mask)
    
    # Save the processed image to the specified path    (output_path)

# Example usagesource_img = r'D:\test_pic\'  # Source image pathoutput_img = r'D:\test_pic\20250306_output.png'         # Output image pathround_corners(source_img, output_img, 300)            # Call the function and set the rounded corner radius to 300

Practical operation

You can follow the above example code, replace the source image path and output image path with your own file path, and adjust the value of the rounded corner radius to achieve the effect you want. After running the code, you will find the processed rounded corner picture in the specified path.

in conclusion

Through the tutorial in this article, we learned how to use the Python programming language combined with the PIL library to add rounded corner effects to images. This method is not only simple and easy to use, but also has significant results. In practical applications, we can adjust the size of the rounded corner radius as needed to achieve different visual effects. Whether it is beautifying images or performing image processing projects, this method is a very practical tool. Hope this article helps you!

The above is the detailed content of the complete code for adding rounded corners to images in Python image processing. For more information about adding rounded corners to Python images, please pay attention to my other related articles!