【1024】用Python画CSDN的默认头像

【1024】用Python画CSDN的默认头像

前言

作为程序员,基本上都用过CSDN。CSDN上有海量的技术博客,也有各种各样的分类社区,提供资源下载等,是非常适合程序员学习和交流的好平台。

1024之际,本文用Python画一张CSDN的默认头像。

进入CSDN首页,可以看到CSDN的图标,如下图。

在这里插入图片描述

如果是新注册的用户,默认的头像跟首页的图标一样。

在这里插入图片描述
在手机上安装CSDN APP,图标也一样。

本文先下载了这张图片,然后用Python实现画图代码,原图如下。

在这里插入图片描述

绘图效果展示

为了先让大家看到绘图的效果,我将绘图过程录制成了视频,效果如下。

【1024】用Python画CSDN的默认头像

实现方法介绍

本文的背景图片用原图一样的背景,背景图片如下。(当然,背景部分也能用turtle画出来)

在这里插入图片描述

本文的绘图工具使用Python的标准库turtle库,无需安装,导入即可使用。

画图用到的函数全部都是turtle中的基础函数。可以参考我的往期画图文章。链接:用Python标准库turtle画一只老虎,祝您新年虎虎生威,大吉大利

部分函数用法介绍(对绘图函数了解可以跳过此部分):

  • 画布设置

title(): 设置窗口的标题。
bgpic():设置背景图片。
setup(width, height, x, y): 设置窗口大小和窗口左上角在屏幕中的位置。
shape():设置鼠标的形状。
done(): 绘图结束后,不自动关闭窗口。

  • 画笔设置

pencolor(color): 设置画笔颜色。
width(): 设置画笔宽度。
speed(int):设置画笔的速度,传入1~10的数字,1最慢,10最快。传入其他值会更快,但是没有鼠标移动的动画效果。
penup():提起画笔,提起后移动画笔不会留下痕迹。
pendown(): 落下画笔,开始绘图前先将画笔落下。
setx(value):设置画笔的x轴坐标。
sety(value): 设置画笔的y轴坐标。
towards(x, y): 设置画笔指向的点。
setheading(): 设置画笔的方向。与towards()配合可以设置画笔指向某个点,如setheading(towards(0,0))可以设置画笔指向原点。
pos(): 返回画笔当前的坐标。鼠标移动一段时间后可以print()打印此函数获取鼠标位置。
heading(): 返回画笔当前的方向。

  • 画笔操作

circle(radius[, extent=arc]):画一个圆,传入圆的半径,根据画笔的方向控制圆心的位置,圆心与画笔的方向垂直。传入弧度值可以画出指定弧度的圆弧。
left(angle):画笔左转,传入任意一个目标角度。
right(angle): 画笔右转。
forward(distance): 画笔前进一段距离。
backward(distance): 画笔后退一段距离。
goto(x, y): 移动画笔到指定坐标。

  • 颜色填充

begin_fill(): 开始填充。
fillcolor(color): 设置图形中填充的颜色。
end_fill(): 结束填充。

具体画图时,调整画笔的起点,设置不同的转角度数和不同的前进长度,可以得到不同形状的弧线,用弧线构成完整的图形。

最终绘图结果与原图对比:

在这里插入图片描述

代码获取

代码共200多行,完整代码如下。

# coding=utf-8
"""
原图:CSDN官方默认图片
代码作者:小斌哥ge
发表日期:2022年10月24日
"""
from turtle import *
import time


def set_start(x, y, w=0.5, c='black'):
    penup()
    setx(x)
    sety(y)
    setheading(towards(0, 0))
    width(w)
    pencolor(c)
    pendown()
    speed(10)


def left_rotate(time, angle, length):
    for i in range(time):
        left(angle)
        forward(length)


def right_rotate(time, angle, length):
    for i in range(time):
        right(angle)
        forward(length)


def fill_color_patch(x, y, c='white'):
    set_start(x, y, 1, c=c)
    forward(1)


def draw_circle(radius, color, color2=''):
    if color2 == '':
        color2 = color
    penup()
    setheading(towards(0, 0))
    right(90)
    pencolor(color)
    pendown()
    begin_fill()
    circle(radius)
    fillcolor(color2)
    end_fill()


def draw_ear():
    # 左耳
    fill_color_patch(-111, 33, c='#F1AE8B')
    begin_fill()
    set_start(-111, 33, w=1, c='#F1AE8B')
    left_rotate(1, 180, 5)
    left_rotate(5, 9, 5)
    left_rotate(5, 6, 5)
    left_rotate(5, 8, 5)
    left_rotate(6, 6, 5)
    left_rotate(6, 8, 5)
    goto(-111, 33)
    fillcolor('#F1AE8B')
    end_fill()
    fill_color_patch(-116, 13, c='#E86E6D')
    begin_fill()
    set_start(-116, 13, w=1, c='#E86E6D')
    left_rotate(1, 180, 5)
    left_rotate(5, 10, 3)
    left_rotate(5, 9, 3)
    left_rotate(5, 7, 3)
    left_rotate(5, 11, 3)
    goto(-116, 13)
    fillcolor('#E86E6D')
    end_fill()
    # 右耳
    fill_color_patch(115, 33, c='#F1AE8B')
    begin_fill()
    set_start(115, 33, w=1, c='#F1AE8B')
    left_rotate(1, 180, 5)
    right_rotate(5, 9, 5)
    right_rotate(5, 6, 5)
    right_rotate(5, 8, 5)
    right_rotate(6, 6, 5)
    right_rotate(6, 8, 5)
    goto(115, 33)
    fillcolor('#F1AE8B')
    end_fill()
    fill_color_patch(120, 13, c='#E86E6D')
    begin_fill()
    set_start(120, 13, w=1, c='#E86E6D')
    left_rotate(1, 180, 5)
    right_rotate(5, 10, 3)
    right_rotate(5, 9, 3)
    right_rotate(5, 7, 3)
    right_rotate(5, 11, 3)
    goto(120, 13)
    fillcolor('#E86E6D')
    end_fill()


def draw_head():
    # 头像轮廓
    fill_color_patch(-111, 33, c='#6A332E')
    begin_fill()
    set_start(-111, 33, w=1, c='#6A332E')
    left_rotate(1, 80, 10)
    right_rotate(5, 5, 13)
    right_rotate(3, 6.5, 15)
    left_rotate(1, 50, 5)
    right_rotate(3, 11, 5)
    right_rotate(3, 5, 5)
    right_rotate(1, 125, 5)
    left_rotate(3, 5, 4)
    left_rotate(3, 46.5, 1.2)
    right_rotate(4, 6, 5.5)
    right_rotate(1, 125, 3)
    left_rotate(3, 3, 5)
    left_rotate(1, 82, 10)
    right_rotate(3, 8.5, 15)
    right_rotate(10, 5.5, 13)
    right_rotate(9, 7, 12)
    left_rotate(2, 10, 3)
    right_rotate(3, 6, 5)
    right_rotate(2, 9, 10)
    right_rotate(16, 1.8, 5)
    right_rotate(4, 7.5, 5.5)
    left_rotate(3, 7, 3.5)
    right_rotate(5, 7.2, 12)
    right_rotate(5, 7, 13)
    right_rotate(2, 5.2, 13)
    goto(-111, 33)
    fillcolor('#6A332E')
    end_fill()
    # 脸
    fill_color_patch(-65, -42, c='#F1AE84')
    begin_fill()
    set_start(-64, -42, w=1, c='#F1AE84')
    left_rotate(1, 128, 10)
    right_rotate(5, 10.8, 10)
    right_rotate(5, 10, 10)
    right_rotate(3, 11, 10)
    right_rotate(3, 8, 10)
    right_rotate(5, 4.5, 5)
    right_rotate(3, 9, 7.8)
    left_rotate(1, 107, 0)
    right_rotate(3, 9, 7.8)
    right_rotate(1, 9, 5)
    right_rotate(4, 4.5, 5)
    right_rotate(1, 4.5, 10)
    right_rotate(2, 7, 10)
    right_rotate(4, 11, 10)
    right_rotate(5, 9.7, 10)
    right_rotate(5, 11, 9.9)
    left_rotate(1, 95, 10)
    right_rotate(5, 6, 8)
    right_rotate(4, 10.7, 7)
    right_rotate(2, 8.7, 10)
    right_rotate(2, 7.5, 10)
    right_rotate(5, 2.6, 10)
    right_rotate(4, 8.5, 10)
    right_rotate(4, 11, 7)
    right_rotate(5, 5.9, 8)
    goto(-64, -42)
    fillcolor('#F1AE84')
    end_fill()


def draw_eye():
    # 左眼
    set_start(-93.5, 8, w=1, c='#411806')
    draw_circle(43, '#411806')
    set_start(-84.5, 6, w=1, c='#D7A69E')
    draw_circle(34, '#D7A69E')
    fill_color_patch(-31, -19.5, c='#C18269')
    begin_fill()
    set_start(-30, -19.5, w=1, c='#C18269')
    left_rotate(3, 15, 9)
    left_rotate(6, 15, 8.2)
    left_rotate(6, 15, 9)
    left_rotate(6, 15, 8.2)
    left_rotate(3, 15, 9)
    fillcolor('#C18269')
    end_fill()
    set_start(-75, 10, w=1, c='#441C14')
    draw_circle(21, '#441C14')
    set_start(-70.5, 9.3, w=1, c='#623831')
    draw_circle(16.5, '#623831')
    set_start(-69, 14, w=1, c='#3E1C1A')
    draw_circle(13, '#3E1C1A')
    set_start(-67, 17.5, w=1, c='#BB7E64')
    draw_circle(5, 'white')
    # 右眼
    set_start(97.5, 7.5, w=1, c='#411806')
    draw_circle(43, '#411806')
    set_start(88.2, 6, w=1, c='#D7A69E')
    draw_circle(34, '#D7A69E')
    fill_color_patch(29.5, 26.5, c='#C18269')
    begin_fill()
    set_start(29.5, 26.5, w=1, c='#C18269')
    left_rotate(3, 15, 8.2)
    left_rotate(6, 15, 9)
    left_rotate(6, 15, 8.2)
    left_rotate(6, 15, 9)
    left_rotate(3, 15, 8.2)
    fillcolor('#C18269')
    end_fill()
    set_start(78, 10, w=1, c='#441C14')
    draw_circle(21, '#441C14')
    set_start(73.8, 9, w=1, c='#623831')
    draw_circle(16, '#623831')
    set_start(69, 16, w=1, c='#3E1C1A')
    draw_circle(13, '#3E1C1A')
    set_start(55, 17.5, w=1, c='#BB7E64')
    draw_circle(5, 'white')
    # 镜托
    fill_color_patch(12, 8, c='#411806')
    begin_fill()
    set_start(12, 8, w=1, c='#411806')
    right_rotate(1, 34, 22)
    left_rotate(1, 90, 7)
    left_rotate(1, 90, 22)
    left_rotate(1, 90, 7)
    fillcolor('#411806')
    end_fill()


def draw_nose_mouth():
    # 鼻子
    set_start(-15.5, -57.5, w=1, c='#662D2A')
    draw_circle(9, '#662D2A')
    set_start(19, -57, w=1, c='#662D2A')
    draw_circle(8.6, '#662D2A')
    # 嘴
    fill_color_patch(21, -96, c='#E26A68')
    begin_fill()
    set_start(21, -96, w=1, c='#E26A68')
    right_rotate(1, 16, 0)
    left_rotate(3, 20, 1.5)
    left_rotate(6, 10, 5.8)
    left_rotate(6, 20, 1.5)
    left_rotate(6, 10, 5.8)
    left_rotate(3, 20, 1.5)
    fillcolor('#E26A68')
    end_fill()


if __name__ == '__main__':
    title('Python画CSDN头像(公众号:小斌哥ge)')
    bgpic(r'csdn_bg.png')
    setup(410, 400, 500, 200)
    shape(name='turtle')
    time.sleep(2)
    draw_ear()
    draw_head()
    draw_eye()
    draw_nose_mouth()
    set_start(500, 500, 2.5)
    done()

想要获取更多Python画图的相关代码,或者有任何画图相关的疑问,可以联系号主。

期待您的三连。

往期推荐:

用Python标准库turtle画一只老虎,祝您新年虎虎生威,大吉大利
用Python标准库turtle画一头金牛,祝您新年牛气冲天

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小斌哥ge

非常感谢,祝你一切顺利。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值