Here is an example code using the Python PIL library to round the corners of an image by applying a mask:
from PIL import Image, ImageDraw
# Open image file
image_path = "path/to/image.jpg"
image = Image.open(image_path)
# Create mask with rounded corners
mask_size = min(image.size)
mask = Image.new('L', (mask_size, mask_size), 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, mask_size, mask_size), fill=255)
mask_corner = (image.size[0] - mask_size) // 2, (image.size[1] - mask_size) // 2
mask_image = Image.new('L', image.size, 0)
mask_image.paste(mask, mask_corner)
# Apply mask to image
image = image.convert('RGBA')
image.putalpha(mask_image)
# Save modified image
image.save("path/to/output.png")
Explanation:
Image.open()
function.'L'
mode) with all pixels set to 0 (black) initially.ImageDraw
module. The circle is centered within the mask image.image.convert()
function. The alpha channel is initialized to 0 for all pixels.image.putalpha()
function. This applies the mask to the image, and non-masked pixels remain fully opaque while masked pixels become increasingly transparent based on the mask pixel values.image.save()
function. The image is saved in PNG format to support transparency.Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-07-30 11:00:00 +0000
Seen: 9 times
Last updated: Feb 11 '23
Can you explain the functioning of the {m,n}? regex in the Python re module?
How can one ensure that sub-classes have uniform method parameters in TypeScript?
How can the calculation of matrix determinant be performed using CUDA?
How can code repetition be prevented when using (box)plot functions?
What steps can I take to prevent my webpage from slowing down when all parts of a div are displayed?
How can circles be detected in openCV?
What is the method to determine the most precise categorization of data using Self Organizing Map?