[PYTHON] Use aggdraw when you want to draw beautifully with pillow

I had a chance to draw a figure using pillow, but when I drew it with the default, it became a fluttering image. If you want to make it clean, you can export it in double size and specify antialiasing when resizing, but it's a bit annoying. I found a module that can be drawn beautifully from the beginning, so I will write it down.

Reference https://stackoverrun.com/ja/q/3880127 Reference https://aggdraw.readthedocs.io/en/latest/

Draw normally with pillow

It looks like this when drawing normally using pollow. Can you see that the horns are not processed and the jagi is out?

from PIL import Image, ImageDraw

image = Image.new('RGB', (300, 300), (255, 255, 255))
draw = ImageDraw.Draw(image)
draw.line((150, 50, 250, 250, 50, 250, 150, 50), fill=(0, 0, 0), width=20)
draw.line((10, 140, 290, 160), fill=(0, 0, 0), width=20)

image.save('test1.png', quality=100)

test1.png

Draw normally with a pillow and round the corners

For the time being, rounding of corners is implemented in the new version of pillow. Just add * joint = "curve" * to the argument.

from PIL import Image, ImageDraw

image = Image.new('RGB', (300, 300), (255, 255, 255))
draw = ImageDraw.Draw(image)
draw.line((150, 50, 250, 250, 50, 250, 150, 50), fill=(0, 0, 0), width=20, joint="curve")
draw.line((10, 140, 290, 160), fill=(0, 0, 0), width=20)

image.save('test2.png', quality=100)

test2.png

This will round the corners. The beginning and end of writing are not processed, so if you're curious, trace the first side again.

from PIL import Image, ImageDraw

image = Image.new('RGB', (300, 300), (255, 255, 255))
draw = ImageDraw.Draw(image)
draw.line((150, 50, 250, 250, 50, 250, 150, 50, 250, 250), fill=(0, 0, 0), width=20, joint="curve")
draw.line((10, 140, 290, 160), fill=(0, 0, 0), width=20)

image.save('test3.png', quality=100)

test3.png

Draw with aggdraw

Installation was normal with pip

pip install aggdraw

With aggdraw, you can draw as it is on the Image created by pillow. It's like defining a pen and writing with it. Can you see that the jagi is gone because it does antialiasing and alpha synthesis on its own?

from PIL import Image
import aggdraw

image = Image.new('RGB', (300, 300), (255, 255, 255))
draw = aggdraw.Draw(image)
pen = aggdraw.Pen((0, 0, 0), 20)
draw.line((150, 50, 250, 250, 50, 250, 150, 50, 250, 250), pen)
draw.line((10, 140, 290, 160), pen)
draw.flush()

image.save('test4.png', quality=100)

test4.png

bonus

There is also a brush (fill)

from PIL import Image
import aggdraw

image = Image.new('RGBA', (300, 300), (255, 255, 255, 255))
draw = aggdraw.Draw(image)
brush = aggdraw.Brush((0, 0, 0), 255)
draw.line((150, 50, 250, 250, 50, 250, 150, 50, 250, 250), brush)
draw.flush()

image.save('test5.png', quality=100)

test5.png

Recommended Posts

Use aggdraw when you want to draw beautifully with pillow
Solution when you want to use cv_bridge with python3 (virtualenv)
When you want to use it as it is when using it with lambda memo
Gist repository to use when you want to try a little with ansible
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
Personal best practice template to use when you want to make MVP with Flask
[AWS] What to do when you want to pip with Lambda
When you want to register Django's initial data with relationships
When you want to use python2.x on modern Gentoo Linux
I know? Data analysis using Python or things you want to use when you want with numpy
If you want to use field names with hyphens when updating firestore data in python
When you want to print to standard output with print while testing with pytest
[Python] When you want to use all variables in another file
When you want to send an object with requests using flask
When you want to adjust the axis scale interval with APLpy
When you want to replace a column with a missing value (NaN) column by column
Memorandum of means when you want to make machine learning with 50 images
I want to use MATLAB feval with python
When you want to update the chrome driver.
I want to use Temporary Directory with Python2
I don't want to use -inf with np.log
I want to use ip vrf with SONiC
How to build an environment when you want to use python2.7 after installing Anaconda3
[OpenCV] When you want to check if it is read properly with imread
If you want to make a discord bot with python, let's use a framework
If you want to use Cython, also include python-dev
Links to do what you want with Sublime Text
When you want to play a game via Proxy
Things to do when you start developing with Django
When you want to plt.save in a for statement
Site notes to help you use NetworkX with Python
The programming language you want to be able to use
[Python] I want to use only index when looping a list with a for statement
When the variable you want to superscript with matplotlib is two or more characters
What to do if you don't want to use Japanese column names when using ortoolpy.logistics_network
[Django] A memorandum when you want to communicate asynchronously [Python3]
I want to use R functions easily with ipython notebook
[python] A note when trying to use numpy with Cython
Useful operation when you want to solve all problems in multiple programming languages with Codewars
[Python] I want to use the -h option with argparse
I want to use a virtual environment with jupyter notebook!
[Python] When you want to import and use your own package in the upper directory
If you want your colleagues to use the same language
[Visualization] I want to draw a beautiful graph with Plotly
When you want to hit a UNIX command on Python
What to do if you get an Undefined error when trying to use pip with pyenv
When you want to use multiple versions of the same Python library (virtual environment using venv)
[Subprocess] When you want to execute another Python program in Python code
If you want to become a data scientist, start with Kaggle
A note on what you did to use Flycheck with Python
I want to use a wildcard that I want to shell with Python remove
I want to use an external library with IBM Cloud Functions
When you want to save the result of the callback function somewhere
Prepare an environment to use OpenCV and Pillow with AWS Lambda
[TensorFlow] If you want to run TensorBoard, install it with pip
When you want to sort a multidimensional list by multiple lines
I really want to use GitHub Flavored Markdown (GFM) with Pelican!
Python: How to use async with
Nice to meet you with python
How to use virtualenv with PowerShell