How do I generate circular image thumbnails using PIL?
The space outside the circle should be transparent.
Snippets would be highly appreciated, thank you in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The easiest way to do it is by using masks. Create a black and white mask with any shape you want. And use
putalphato put that shape as an alpha layer:Here is the mask I used:
If you want the thumbnail size to be variable you can use
ImageDrawand draw the mask:If you want the output in GIF then you need to use the paste function instead of
putalpha:Note that I did the following changes:
was replaced with black and vice versa.
Please note: There is a big issue with this approach. If the GIF image contained black parts, all of them will become transparent as well. You can work around this by choosing another color for the transparency.
I would strongly advise you to use PNG format for this. But if you can’t then that is the best you could do.