Jun 01, 2021 Article blog
This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column
Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn
Share a recent wave of Get a python painting method.
Let's get off to a happy start!
Baidu web download link:
https://pan.baidu.com/s/1ZBs4-DpVxXvoisAllwHV5Q
Password: 9nei
Python version: 3.6.4
Related modules:
cv2 module;
numpy module;
pywin32 module;
bs4 module;
and some Python's own modules.
other:
potrace
Environment construction
Install Python and add it to the environment variable, and pip installs the relevant modules that are required.
Resources
1.http://www.html-js.com/article/1628
2.https://zh.wikipedia.org/wiki/%E5%8F%AF%E7%B8%AE%E6%94%BE%E5%90%91%E9%87%8F%E5%9C%96%E5%BD%A2
3.https://www.cnblogs.com/hnfxs/p/3148483.html
Take a quick look
Modify the path to the picture you want to draw:
Run the main.py file in the cmd window.
The effect is as follows:
I. Implementation steps
First read the original image:
Secondly, the number of colors of the original image is reduced to the specified number by the K mean cluster, the larger the K value, the slower the operation, but the better the effect:
Each time you remove a color from the clustering results and use potrace to convert it to a graphic in SVG format, parse the format and draw it with Python's own gallery:
Two .SVG format
SVG, which scales vector graphics, is a graphics format based on a scalable markup language (XML) that describes 2D vector graphics. SVG mainly supports the following display objects:
1. Vector display objects, basic vector display objects include rectangles, circles, ellipses, polygons, straight lines, arbitrary curves, etc
2. Embedded external images, including PNG, JPEG, SVG, etc
3. Text object.
For more technical details on SVG, please refer to:
http://www.w3school.com.cn/svg/svg_intro.asp
Iii. Turtle Library
Here's a recommended document for the Turtle library:
https://www.rddoc.com/doc/Python/3.6.0/zh/library/turtle/
Bezier curve
The Bezier curve is used for drawing, which is briefly described here.
The mathematical basis of the Bezier curve is the Bernstein polynomial, named after the French engineer Pierre Bézier.
Bezier curve control is simple but has strong description ability, so it is widely used in the field of industrial design, and bezier curve also plays an important role in vector graphics. Some of our most common vector drawing software today (e.g. Flash, CorelDraw, PS, etc.) provides the ability to plot Bezier curves.
Linear formula:
Given points P0, P1, the linear Bezier curve is a straight line between two points, determined as follows:
It's actually linear interpolation.
Secondary formula:
Given points P0, P1, and P2, the path of the second-order Bezier curve is determined as follows:
n-side formula:
Given points P0 to Pn, the path of the n-order Bezier curve is determined as follows:
How the Bezier curve is drawn (in the second order):
Suppose the three points in the plane that are not collinear are shown in the following illustration:
Select a little D on AB and A little E on BC to make:
AD:AB=BE:BC。
Connect DE:
Select a little F on the DE so that:
AD:AB=BE:BC=DF:DE
To ensure that the proportional relationship between AD, AB, BE, BC, DF, and DE remains unchanged, and that the D-point moves from point A to point B, the curve of all point F is the second-order Bezier curve:
The n-order Bezier curve is drawn in a similar way, for example, in the third and fourth orders:
That's all~
Complete source code and required tools are available in the relevant files, please download and use them yourself.
The code is correct as of 2018-07-31.
Still quite interesting, the original picture is best not too big, otherwise the generation speed is very slow very slow very slow.