關于Python使用turtle庫畫任意圖的問題
環(huán)境配置
系統(tǒng):Windows10
版本:python 3.8
Turtle掃盲
1.繪圖窗體的設置
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),直線前進d(可為負數(shù))個像素 turtle.circle(r, extent=None) #根據(jù)半徑r繪制extent角度的弧形 turtle.setheading(angle) #別名 turtle.seth(angle),angle: 行進方向的絕對角度 turtle.left(angle) #海龜向左轉(zhuǎn),angle: 在海龜當前行進方向上旋轉(zhuǎn)的角度 turtle.right(angle) #海龜向右轉(zhuǎn) turtle.goto(x, y) # 絕對坐標
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] #填入你的圖片絕對路徑,建議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() # 保持界面顯示
到此這篇關于Python turtle庫畫任意圖的文章就介紹到這了,更多相關Python turtle庫畫圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用Pexpect庫實現(xiàn)自動化與終端交互的任務
Pexpect 是一個 Python 庫,用于自動化與終端交互的任務,它提供了一種簡單的方式來編寫腳本,以便與終端程序進行交互,下面我們就來深入了解一下Pexpect庫的具體使用吧2023-12-12Python的爬蟲包Beautiful Soup中用正則表達式來搜索
這篇文章主要介紹了Python的爬蟲包Beautiful Soup中用正則表達式來搜索的技巧,包括使用正則表達式去搜索多種可能的關鍵字以及查找屬性值未知的標簽等,需要的朋友可以參考下2016-01-01解決windows上安裝tensorflow時報錯,“DLL load failed: 找不到指定的模塊”的問題
這篇文章主要介紹了解決windows上安裝tensorflow時報錯,“DLL load failed: 找不到指定的模塊”的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Python操作PDF實現(xiàn)制作數(shù)據(jù)報告
Python操作PDF的庫有很多,比如PyPDF2、pdfplumber、PyMuPDF等等。本文將利用FPDF模塊操作PDF實現(xiàn)制作數(shù)據(jù)報告,感興趣的小伙伴可以嘗試一下2022-12-12