日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

關(guān)于Python使用turtle庫(kù)畫任意圖的問(wèn)題_python

作者:Mosu's_tech_blog ? 更新時(shí)間: 2022-06-02 編程語(yǔ)言

環(huán)境配置

系統(tǒng):Windows10

版本:python 3.8

Turtle掃盲

1.繪圖窗體的設(shè)置

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

startx , starty 缺省在屏幕中心。

2.畫筆控制函數(shù)

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的小數(shù)值: turtle.pencolor(0.63, 0.13, 0.94)
RGB的元組值: turtle.pencolor((0.63,0.13,0.94))

3.形狀繪制函數(shù)

turtle.forward(d) #別名 turtle.fd(d),直線前進(jìn)d(可為負(fù)數(shù))個(gè)像素
turtle.circle(r, extent=None) #根據(jù)半徑r繪制extent角度的弧形
turtle.setheading(angle) #別名 turtle.seth(angle),angle: 行進(jìn)方向的絕對(duì)角度
turtle.left(angle) #海龜向左轉(zhuǎn),angle: 在海龜當(dāng)前行進(jìn)方向上旋轉(zhuǎn)的角度
turtle.right(angle) #海龜向右轉(zhuǎn)
turtle.goto(x, y) # 絕對(duì)坐標(biāo)

Turtle畫任意圖

1.經(jīng)典案例

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() # 保持界面顯示,后面的語(yǔ)句失效

2.畫任意圖片

import turtle as t
import cv2
t.getscreen().colormode(255)
img1 = cv2.imread('2.jpg')[0: -2: 2] #填入你的圖片絕對(duì)路徑,建議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

欄目分類
最近更新