刚刚,
被《啥是佩奇》这支广告片刷屏了。
佩奇明明是个喜剧角色,
却看哭了所有人。
备注:大家想看详细视频,可网上搜索观看,此处只截图留样说明
今天分享的主题是:如何使用python绘画小猪佩奇。
1、首先来讲下具体的实现思路:
选好画板大小,设置好画笔颜色,粗细,定位好位置,依次画鼻子,头、耳朵,眼睛,腮,嘴,身体,手脚,尾巴,完事。
都知道,turtle 是 python 内置的一个比较有趣味的模块,俗称 海龟绘图,它是基于 tkinter 模块打造,提供一些简单的绘图工具。
在海龟作图中,我们可以编写指令让一个虚拟的(想象中的)海龟在屏幕上来回移动。这个海龟带着一只钢笔,我们可以让海龟无论移动到哪都使用这只钢笔来绘制线条。通过编写代码,以各种很酷的模式移动海龟,我们可以绘制出令人惊奇的图片。使用海龟作图,我们不仅能够只用几行代码就创建出令人印象深刻的视觉效果,而且还可以跟随海龟看看每行代码如何影响到它的移动。这能够帮助我们理解代码的逻辑。所以海龟作图也常被用作新手学习 Python 的一种方式。更丰富详细的功能及知识可以参考:https://docs.python.org/3/library/turtle.html
了解了tuttle的用法之后就可以开始实战了。
2、具体代码实现如下【Python3版本】:
import turtle def nose(x, y):#鼻子 turtle.penup()#提起笔 turtle.goto(x,y)#定位 turtle.pendown()#落笔,开始画 turtle.setheading(-30)#将乌龟的方向设置为to_angle/为数字(0-东、90-北、180-西、270-南) turtle.begin_fill()#准备开始填充图形 a = 0.4 for i in range(120): if 0 <= i < 30 or 60 <= i < 90: a = a+0.08 turtle.left(3) #向左转3度 turtle.forward(a) #向前走a的步长 else: a = a-0.08 turtle.left(3) turtle.forward(a) turtle.end_fill()#填充完成 turtle.penup() turtle.setheading(90) turtle.forward(25) turtle.setheading(0) turtle.forward(10) turtle.pendown() turtle.pencolor(255,155,192)#画笔颜色 # turtle.color("pink") turtle.setheading(10) turtle.begin_fill() turtle.circle(5) turtle.color(160,82,45)#返回或设置pencolor和fillcolor turtle.end_fill() turtle.penup() turtle.setheading(0) turtle.forward(20) turtle.pendown() turtle.pencolor(255,155,192) turtle.setheading(10) turtle.begin_fill() turtle.circle(5) turtle.color(160,82,45) turtle.end_fill() def head(x,y):#头 turtle.color((255,155,192),"pink") turtle.penup() turtle.goto(x,y) turtle.setheading(0) turtle.pendown() turtle.begin_fill() turtle.setheading(180) turtle.circle(300,-30) turtle.circle(100,-60) turtle.circle(80,-100) turtle.circle(150,-20) turtle.circle(60,-95) turtle.setheading(161) turtle.circle(-300,15) turtle.penup() turtle.goto(-100,100) turtle.pendown() turtle.setheading(-30) a = 0.4 for i in range(60): if 0 <= i < 30 or 60 <= i < 90: a = a+0.08 turtle.lt(3)#向左转3度 turtle.fd(a)#向前走a的步长 else: a = a-0.08 turtle.lt(3) turtle.fd(a) turtle.end_fill() def ears(x,y): #耳朵 turtle.color((255,155,192),"pink") turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.begin_fill() turtle.setheading(100) turtle.circle(-50,50) turtle.circle(-10,120) turtle.circle(-50,54) turtle.end_fill() turtle.penup() turtle.setheading(90) turtle.forward(-12) turtle.setheading(0) turtle.forward(30) turtle.pendown() turtle.begin_fill() turtle.setheading(100) turtle.circle(-50,50) turtle.circle(-10,120) turtle.circle(-50,56) turtle.end_fill() def eyes(x,y):#眼睛 turtle.color((255,155,192),"white") turtle.penup() turtle.setheading(90) turtle.forward(-20) turtle.setheading(0) turtle.forward(-95) turtle.pendown() turtle.begin_fill() turtle.circle(15) turtle.end_fill() turtle.color("black") turtle.penup() turtle.setheading(90) turtle.forward(12) turtle.setheading(0) turtle.forward(-3) turtle.pendown() turtle.begin_fill() turtle.circle(3) turtle.end_fill() turtle.color((255,155,192),"white") turtle.penup() turtle.seth(90) turtle.forward(-25) turtle.seth(0) turtle.forward(40) turtle.pendown() turtle.begin_fill() turtle.circle(15) turtle.end_fill() turtle.color("black") turtle.penup() turtle.setheading(90) turtle.forward(12) turtle.setheading(0) turtle.forward(-3) turtle.pendown() turtle.begin_fill() turtle.circle(3) turtle.end_fill() def cheek(x,y):#腮 turtle.color((255,155,192)) turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.setheading(0) turtle.begin_fill() turtle.circle(30) turtle.end_fill() def mouth(x, y): #嘴 turtle.color(239,69,19) turtle.penup() turtle.goto(x,y) turtle.pendown() turtle.setheading(-80) turtle.circle(30,40) turtle.circle(40,80) def body(): #身体 turtle.color("red", (255, 99, 71)) turtle.pu() turtle.seth(90) turtle.fd(-20) turtle.seth(0) turtle.fd(-78) turtle.pd() turtle.begin_fill() turtle.seth(-130) turtle.circle(100, 10) turtle.circle(300, 30) turtle.seth(0) turtle.fd(230) turtle.seth(90) turtle.circle(300, 30) turtle.circle(100, 3) turtle.color((255, 155, 192), (255, 100, 100)) turtle.seth(-135) turtle.circle(-80, 63) turtle.circle(-150, 24) turtle.end_fill() def hands(): # 手 turtle.color((255, 155, 192)) turtle.pu() turtle.seth(90) turtle.fd(-40) turtle.seth(0) turtle.fd(-27) turtle.pd() turtle.seth(-160) turtle.circle(300, 15) turtle.pu() turtle.seth(90) turtle.fd(15) turtle.seth(0) turtle.fd(0) turtle.pd() turtle.seth(-10) turtle.circle(-20, 90) turtle.pu() turtle.seth(90) turtle.fd(30) turtle.seth(0) turtle.fd(237) turtle.pd() turtle.seth(-20) turtle.circle(-300, 15) turtle.pu() turtle.seth(90) turtle.fd(20) turtle.seth(0) turtle.fd(0) turtle.pd() turtle.seth(-170) turtle.circle(20, 90) def foots():# 脚 turtle.pensize(10) turtle.color((240, 128, 128)) turtle.pu() turtle.seth(90) turtle.fd(-75) turtle.seth(0) turtle.fd(-180) turtle.pd() turtle.seth(-90) turtle.fd(40) turtle.seth(-180) turtle.color("black") turtle.pensize(15) turtle.fd(20) turtle.pensize(10) turtle.color((240, 128, 128)) turtle.pu() turtle.seth(90) turtle.fd(40) turtle.seth(0) turtle.fd(90) turtle.pd() turtle.seth(-90) turtle.fd(40) turtle.seth(-180) turtle.color("black") turtle.pensize(15) turtle.fd(20) def tail():# 尾巴 turtle.pensize(4) turtle.color((255, 155, 192)) turtle.pu() turtle.seth(90) turtle.fd(70) turtle.seth(0) turtle.fd(95) turtle.pd() turtle.seth(0) turtle.circle(70, 20) turtle.circle(10, 330) turtle.circle(70, 30) def setting(): #参数设置 turtle.pensize(4) turtle.hideturtle() #使乌龟无形(隐藏) turtle.colormode(255) #将其设置为1.0或255.随后 颜色三元组的r,g,b值必须在0 .. cmode范围内 turtle.color((255,155,192),"pink") turtle.setup(840, 500) turtle.speed(10) def main(): setting() #画布、画笔设置 nose(-100,100) #鼻子 head(-69,167) #头 ears(0,160) #耳朵 eyes(0,140) #眼睛 cheek(80,10) #腮 mouth(-20,30) #嘴 body() #身体 hands() #手 foots() #脚 tail() #尾巴 turtle.done() if __name__ == '__main__': main()
每一个方法都会实现不同部位的绘制功能,请见注释说明。
3、具体绘制效果图如下:
感兴趣的朋友,可以复制代码在本地运行起来,看下绘制效果。
备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:
添加关注,一起共同成长吧。