Generate rounded thumbnails in Python

How to generate a rounded image in Python.

Basically, I make the original image square and mask it with a circle, but if I use PIL (Pillow) Image, ImageDraw, etc., antialiasing does not work and I can only get through dirty.

With PIL (Pillow), it seems that only resize and thumbnail can set antialiasing and filters. stackoverflow

The solution is as follows.

from PIL import Image, ImageOps
mask = Image.open("Mask image path")
org = Image.open("Original image path")
sq = ImageOps.fit(sq, (400, 400), method = Image.LANCZOS)
sq.putalpha(mask.convert("L"))
sq.save("Path to save the image cut out in a circle")

Referenced page. stackoverflow

You can also use image libraries such as Cairo and aggdraw. However, I didn't use it because it doesn't seem to be maintained. Quora

Recommended Posts

Generate rounded thumbnails in Python
Generate U distribution in Python
Generate QR code in Python
Generate 8 * 8 (64) cubes in Blender Python
[Python] Generate QR code in memory
Generate Jupyter notebook ".ipynb" in Python
Generate a first class collection in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Generate AWS-S3 signed (time-limited) URLs in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
Automatically generate Python Docstring Comment in Emacs
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Module to generate word N-gram in Python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
Generate a class from a string in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Generate C language from S-expressions in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python
flatten in python
Generate the Look-and-say Sequence featured in QuizKnock in Python
How to generate permutations in Python and C ++
Generate points only in Tokyo using python + shapely
Sorted list in Python
Clustering text in Python
Daily AtCoder # 2 in Python