python使用海龟turtle实现绘制汉字、中文

一、实现要求

        使用python中的turtle库绘制指定汉字、中文

二、实现思路

       1、要想实现汉字的绘制,首先需要知道汉字的笔画坐标,汉字的笔画坐标在网上有,需要使用爬虫技术抓取到指定汉字的笔画坐标信息

  2、根据汉字的笔画坐标信息,使用turrle绘制出相应的笔画,即可实现汉字的绘制

三、代码实现

1、导入需要使用到的模块信息:

import turtle
import requests
from urllib.parse import quote
import re

   

2、首先,汉字的笔画坐标信息,可以在这里获取到:

https://bihua.bmcx.com/web_system/bmcx_com_www/system/file/bihua/get_0/

    因此编写函数向该网址发送数据请求信息:

ef get_word_coordinate(target_word):
    """
    获取指定汉字的笔画坐标
    :param target_word:
    :return:
    """
    url = "https://bihua.bmcx.com/web_system/bmcx_com_www/system/file/bihua/get_0/"

    params = {
        'font': quote(target_word).replace("%", "").lower(),
        'shi_fou_zi_dong': '1',
        'cache_sjs1': '20031914',
    }
    response = requests.get(url, params=params)
    content = response.text
    content = content.replace('hzbh.main(', '').split(');document.getElementById')[0]
    content = content.split('{')[-1].split("}")[0]
    pattern = re.compile(r'\w:\[(.+?)\]')
    result = re.split(pattern, content)
    order_xy_routine = []
    words_cnt = 0
    for r in result:
        sec = re.findall(r'\'.+?\'', r)
        if len(sec):
            orders = sec[1].split('#')
            for order in orders:
                order_str = re.findall(r'\(\d+,\d+\)', order)
                order_xy = [eval(xy) for xy in order_str]
                order_xy_routine.append(order_xy)
            words_cnt += 1
    print(order_xy_routine)
    return order_xy_routine

  该请求需要传递三个参数。数据的发回结果是一个html格式的数据,需要使用到正则表达式进行笔画坐标的解析和处理,最终得到指定汉字的坐标信息,是一个er二维的列表,形式如下所示:

[[(666, 36), (696, 66), (480, 108), (300, 138)], [(510, 102), (510, 558)], [(510, 318), (690, 318), (648, 300), (606, 318)], [(342, 234), (372
, 258), (372, 558), (342, 582), (372, 558), (696, 558), (648, 540), (600, 558)], [(36, 114), (228, 114), (270, 84), (228, 114), (90, 330), (48
, 354), (90, 330), (246, 330), (288, 300), (246, 330), (210, 522), (192, 594), (162, 642), (120, 690), (30, 750)], [(84, 390), (120, 474), (15
6, 540), (204, 600), (270, 654), (318, 678), (372, 690), (726, 702)]]
 

3、编写函数,调用汉字笔画坐标的获取方法,根号就坐标信息,实现指定汉字的绘制

def draw_words(target_words, startx, starty):
    """
    绘制汉字
    :param target_words:
    :param startx:
    :param starty:
    :return:
    """
    turtle.color("black", "black")  # 设置画笔颜色
    turtle.pu()  # 抬起画笔
    coordinates = get_word_coordinate(target_words)
    for index, coordinate in enumerate(coordinates):
        turtle.goto((startx + coordinate[0][0]) / 2, -(starty + coordinate[0][1]) / 2)
        turtle.pd()
        for xy in coordinate:
            x, y = xy
            turtle.goto((startx + x) / 2, -(starty + y) / 2)
        turtle.pu()

  传入三个参数,第一个参数是要绘制的汉字,一次只能一个,如果要实现绘制多个,则调用该方法多次。第二三个参数分别是绘制汉字的起始x、y坐标,用于控制汉字绘制的位置。

4、编写主函数,调用绘制汉字的方法,传入要绘制的汉字,实现汉字的绘制效果

if __name__ == '__main__':
    #要绘制的汉字
    words = ['你','好']
    start_x = -900
    for w in words:
        draw_words(w, start_x, -300)
        start_x+=800
    turtle.done()

  注意画笔的坐标要设置为负数。

四、运行效果

 1、

2、 

 3、

 

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2023年11月3日
下一篇 2023年11月3日

相关推荐