[PYTHON] Draw a beautiful circle with numpy

The OpenCV circle is suitable, so let's draw a cleaner circle. The image of a circle with radius r is 1 at a distance less than r-1 from the center, 0 at a distance greater than r, and r-distance between them.

import math, cv2, numpy as np
# r must be greater than 0.
def circle(r):
    c = math.ceil(r - 1)
    s = c * 2 + 1
    return np.clip(r - np.sqrt(np.sum((np.stack((
        np.tile(np.arange(s), (s, 1)),
        np.repeat(np.arange(s), s).reshape((-1, s))
    )) - c) ** 2, axis=0)), 0, 1)
# r must be greater than 0. width must be greater than 0 and less than r.
def outline_circle(r, width):
    circ = circle(r)
    icirc = circle(r - width)
    ch, cw = circ.shape
    ich, icw = icirc.shape
    sx = (cw - icw) // 2
    sy = (ch - ich) // 2
    ex = sx + icw
    ey = sy + ich
    circ[sy:ey, sx:ex] = np.amax(np.stack((circ[sy:ey, sx:ex] - icirc, np.zeros((ich, icw)))), axis=0)
    return circ
def save_cv2_circle(file_name, r, thickness = -1):
    s = r * 2 + 1
    cv2.imwrite(file_name, cv2.circle(np.zeros((s, s), dtype=np.uint8), (r, r), r, 255, thickness))
def save_circle(file_name, c):
    im = (np.around(c) * 255).astype(np.uint8)
    cv2.imwrite(file_name, im)
save_circle('/tmp/circle10.jpg', circle(11))
save_cv2_circle('/tmp/cv2_circle10.jpg', 10)
save_circle('/tmp/circle10_outline.jpg', outline_circle(11, 1))
save_cv2_circle('/tmp/cv2_circle10_outline.jpg', 10, 1)

/tmp/circle10.jpg circle10.jpg

/tmp/cv2_circle10.jpg cv2_circle10.jpg

/tmp/circle10_outline.jpg circle10_outline.jpg

/tmp/cv2_circle10_outline.jpg cv2_circle10_outline.jpg

The difference is obvious.

Recommended Posts

Draw a beautiful circle with numpy
Draw a graph with NetworkX
Draw a graph with networkx
Draw a graph with Julia + PyQtGraph (2)
Draw a loose graph with matplotlib
Draw a graph with Julia + PyQtGraph (1)
Draw a graph with Julia + PyQtGraph (3)
Draw a graph with pandas + XlsxWriter
[Visualization] I want to draw a beautiful graph with Plotly
Draw a graph with PySimple GUI
Easily draw a map with matplotlib.basemap
Draw a Mandelbrot set with Brainf * ck
I made a life game with Numpy
Read a character data file with numpy
Draw a graph with PyQtGraph Part 1-Drawing
Draw a flat surface with a matplotlib 3d graph
Draw a graph with Japanese labels in Jupyter
How to draw a 2-axis graph with pyplot
Find a position above the threshold with NumPy
Draw a graph by processing with Pandas groupby
[Python] Draw a directed graph with Dash Cytoscape
Try to draw a life curve with python
[Python] Draw a Mickey Mouse with Turtle [Beginner]
Draw a graph with PyQtGraph Part 4-PlotItem settings
Draw a graph with matplotlib from a csv file
Draw a graph with PyQtGraph Part 6-Displaying a legend
I made a random number graph with Numpy
Draw a graph with PyQtGraph Part 5-Increase the Y-axis
[Python] Draw a Qiita tag relationship diagram with NetworkX
I learned Python with a beautiful girl at Paiza # 02
[Python] How to draw a line graph with Matplotlib
I learned Python with a beautiful girl at Paiza # 01
A4 size with python-pptx
I tried to draw a route map with Python
Extract elements other than a specific index with Numpy
Moving average with numpy
Forcibly draw something like a flowchart with Python, matplotlib
Getting Started with Numpy
[Python] Delete by specifying a tag with Beautiful Soup
Learn with Cheminformatics NumPy
Matrix concatenation with Numpy
Hamming code with numpy
Draw a graph with PyQtGraph Part 2--Detailed plot settings
Scraping with Beautiful Soup
Regression analysis with NumPy
Decorate with a decorator
[Python] How to draw a scatter plot with Matplotlib
Extend NumPy with Rust
Study math with Python: Draw a sympy (scipy) graph with matplotlib
NumPy zeros can be defined even with a size of 0
Let's create a PRML diagram with Python, Numpy and matplotlib.
[python] A note when trying to use numpy with Cython
How to extract other than a specific index with Numpy
Kernel regression with Numpy only
Draw Bezier curves with Go
I wrote GP with numpy
Learn librosa with a tutorial 1
CNN implementation with just numpy
Artificial data generation with numpy
Draw hello world with mod_wsgi
Try programming with a shell!