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')
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: 2023-07-17 19:00:43 +0000
Seen: 13 times
Last updated: Jul 17 '23
What is the correct method of aligning PIL text to the center?
What is the method to ensure an image remains between two <br> tags regardless of the screen size?
What is the method for setting a dynamic background image in Vue?
What is the method for aligning an image at the center using NPOI in c#?
How can I use iText7 to create a PDF that includes a pie chart?
What is causing my Flutter Android apk's image to appear as a single 1x1 pixel?
When image delivery is slow, how does it affect the inference time with GPU?