网络编程
位置:首页>> 网络编程>> Python编程>> 关于Python使用turtle库画任意图的问题

关于Python使用turtle库画任意图的问题

作者:Mosu's_tech_blog  发布时间:2023-08-21 14:02:34 

标签:Python,turtle库,画图

环境配置

系统:Windows10

版本:python 3.8

Turtle扫盲

1.绘图窗体的设置

turtle.setup(width, height, startx, starty)

startx , starty 缺省在屏幕中心。

2.画笔控制函数

turtle.penup() #别名 turtle.pu(),抬起画笔
turtle.pendown() #别名 turtle.pd(),落下画笔
turtle.pensize(width) #别名 turtle.width(width),画笔宽度
turtle.pencolor(color) #color为颜色字符串或r,g,b值,画笔颜色

注:

颜色字符串 : turtle.pencolor("purple")
RGB的小数值: turtle.pencolor(0.63, 0.13, 0.94)
RGB的元组值: turtle.pencolor((0.63,0.13,0.94))

3.形状绘制函数

turtle.forward(d) #别名 turtle.fd(d),直线前进d(可为负数)个像素
turtle.circle(r, extent=None) #根据半径r绘制extent角度的弧形
turtle.setheading(angle) #别名 turtle.seth(angle),angle: 行进方向的绝对角度
turtle.left(angle) #海龟向左转,angle: 在海龟当前行进方向上旋转的角度
turtle.right(angle) #海龟向右转
turtle.goto(x, y) # 绝对坐标

Turtle画任意图

1.经典案例

import turtle as t
t.setup(650,650,200,200)
t.speed(10) # 画笔的速度,1到10递增
t.penup()
t.fd(-250)
t.pendown()
t.pensize(25)
t.pencolor("purple")
t.seth(-40)
for i in range(4):
t.circle(40, 80)
t.circle(-40, 80)
t.circle(40, 80/2)
t.fd(40)
t.circle(16, 180)
t.fd(40 * 2/3)
t.mainloop() # 保持界面显示,后面的语句失效

2.画任意图片

import turtle as t
import cv2
t.getscreen().colormode(255)
img1 = cv2.imread('2.jpg')[0: -2: 2] #填入你的图片绝对路径,建议100kb以下。
width = len(img1[0])
height = len(img1)
t.setup(width=width / 2 + 100, height=height + 100)
t.speed(8)
t.pu()
t.goto(-width / 4 + 10, height / 2 - 10)
t.pd()
t.tracer(2000)
for k1, i in enumerate(img1):
   for j in i[::2]:
       t.pencolor((j[0], j[1], j[2]))
       t.fd(1)
   t.pu()
   t.goto(-width / 4 + 10, height / 2 - 10 - k1 - 1)
   t.pd()
t.done() # 保持界面显示

来源:https://www.cnblogs.com/mosu/p/16085184.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com