##Round corner images
from PIL import Image, ImageDraw, ImageFilter

def add_shadow_with_rounded_corners(image_path, output_path, corner_radius, shadow_size):
# Open the original image
img = Image.open(image_path).convert("RGBA")
original_size = img.size
scaled_size = (original_size[0] - 2 * shadow_size, original_size[1] - 2 * shadow_size)
# Scale down the image
img = img.resize(scaled_size, Image.ANTIALIAS)
# Create a mask for the rounded corners
mask = Image.new("L", scaled_size, 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle((0, 0, scaled_size[0], scaled_size[1]), radius=corner_radius, fill=255)
# Apply the rounded corner mask to the image
rounded_img = Image.new("RGBA", scaled_size)
rounded_img.paste(img, (0, 0), mask=mask)
# Create the shadow background
shadow = Image.new("RGBA", original_size, (0, 0, 0, 0))
draw = ImageDraw.Draw(shadow)
draw.rounded_rectangle(
(shadow_size, shadow_size, original_size[0] - shadow_size, original_size[1] - shadow_size),
radius=corner_radius,
fill=(0, 0, 0, 128) # Semi-transparent black shadow
)
shadow = shadow.filter(ImageFilter.GaussianBlur(radius=shadow_size // 2))
# Create the final image
output = Image.new("RGBA", original_size, (0, 0, 0, 0))
output.paste(shadow, (0, 0))
output.paste(rounded_img, (shadow_size, shadow_size), mask=mask)
# Save the output image
output.save(output_path, format="PNG")
print(f"Image with shadow and rounded corners saved at: {output_path}")

# Usage
input_image = '/Users/jitendersingh/Downloads/-original-imagvfy8jxgzxh5e.jpeg'
output_image = "/Users/jitendersingh/Downloads/rounded_image.png" # Replace with desired output file path
corner_radius = 50 # Radius for rounded corners
shadow_size = 50 # Size of the shadow
add_shadow_with_rounded_corners(input_image, output_image, corner_radius, shadow_size)


0 comments:

Post a Comment

 
Top