1 | initial version |
To create circular thumbnails using PIL, you need to follow these steps:
Image.open()
method.Image.resize()
method.Image.new()
method and ImageDraw.Draw()
method. Set the mode to 'RGBA' to include the alpha channel for transparency.ImageDraw.Draw.ellipse()
method.putalpha()
method to apply the alpha channel to the circular shape.Image.paste()
method.Image.save()
method.Here is an example code:
from PIL import Image, ImageDraw
# Open the image
image = Image.open('example.jpg')
# Define the radius of the circular thumbnail
radius = 100
# Resize the image
image = image.resize((radius*2, radius*2))
# Create a circular image
mask = Image.new('RGBA', (radius*2, radius*2), (255, 255, 255, 0))
mask_draw = ImageDraw.Draw(mask)
mask_draw.ellipse((0, 0, radius*2, radius*2), fill=(255, 255, 255, 255))
# Apply the alpha channel to the circular image
mask.putalpha(mask_draw)
# Paste the resized image onto the circular image
image.putalpha(mask)
# Save the circular thumbnail as PNG format
image.save('circular_thumbnail.png', format='PNG')