溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python使用turtle庫畫任意圖的方法

發(fā)布時(shí)間:2022-04-01 11:06:47 來源:億速云 閱讀:284 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Python使用turtle庫畫任意圖的方法”,在日常操作中,相信很多人在Python使用turtle庫畫任意圖的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”Python使用turtle庫畫任意圖的方法”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

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

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() # 保持界面顯示

到此,關(guān)于“Python使用turtle庫畫任意圖的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI