Turtle library drawing: draw all QQ expressions

Turtle library drawing: draw QQ emoticons Resource links are on the right: Turtle library drawing: draw QQ emoticons

Implementation ideas

main idea

The turtle brush slides on the image matrix, sets the pen color to the RGB value of the current pixel through the pencolor() function, draws a certain distance, and then obtains the distance of the next pixel to continue drawing.

Improve

Since each pixel is drawn, the drawing time of the whole expression will be relatively long. In order to further improve the drawing speed, a pre-judgment process is added before drawing. If the average RGB of 5 consecutive pixels<=0.9, it is correct. These five pixel points are drawn respectively, otherwise the pre-judgment of the next five pixel points is performed. The addition of pre-judgment can reduce the drawing of some background pixels and save some time.

Optional parameters and recommended settings

im: the emoticon picture to be drawn locally read in
ps: the brush size when drawing
stepsize: the pixel setting when pre-judging

It is recommended that the resolution of the emoticon image read in is less than 250×250, and the brush size is less than 5. The brush size is the magnification of the original image. If the setting is too large, the jaggedness will be very obvious. At the same time, the product of the brush size and the original image resolution determines the The size of the drawing window, if it is too large, the canvas will be displayed incompletely.
The pre-judgment threshold is set to 0.9, that is, the background pixels in the image are considered to be close to white, so for images with backgrounds of other colors, this threshold will not work, and the program can also realize the drawing of such images, but it cannot save drawing time.

specific code

# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 21:34:21 2018

@author: 魅梦
"""
import matplotlib.pyplot as plt
from turtle import *
import numpy as np

im = plt.imread('20201114102455.png')
speed(0)
# 设置画笔大小
ps = 3
stepsize = 5
height, weight, deep = im.shape[0], im.shape[1], im.shape[2]
maincolor = np.mean(im.reshape(height*weight, deep), axis=0)
print(tuple(maincolor))
if im[0][0][0] > 1:
    im = im/255
screensize(weight*ps, height*ps)
setup(weight*(ps + 1), height*(ps + 1))

for he in range(height):
    for we in range(0, weight, stepsize):
        if np.mean(im[he][we: we + stepsize]) <= 0.9:
            up()
            goto(ps*(we - weight/2), ps*(height/2 - he))
            down()
            for w in range(we, we + stepsize):
                r, g, b = im[he][w][0], im[he][w][1], im[he][w][2]
                pencolor(r.item(), g.item(), b.item())
                pensize(ps)
                forward(ps)
hideturtle()
done()

achieve effect

Expression drawing result

Direction can be improved

This article realizes the drawing of expressions. In order to save the drawing time of some images, pre-judgment is made. Some parameters are optional. If readers are interested, they can introduce image denoising algorithm to realize high-definition drawing of expressions, and at the same time, the overall background RGB Perform extraction to realize the automatic setting of the pre-judgment threshold and expand the applicable image range of the program.

Guess you like

Origin blog.csdn.net/qq_36949278/article/details/110672463