怎么用python繪制一個(gè)小豬佩奇

這篇文章主要介紹“怎么用python繪制一個(gè)小豬佩奇”,在日常操作中,相信很多人在怎么用python繪制一個(gè)小豬佩奇問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么用python繪制一個(gè)小豬佩奇”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供競(jìng)秀企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為競(jìng)秀眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

1.利用python繪制一個(gè)小豬佩奇

turtle庫(kù)是一個(gè)很好的python圖形繪制庫(kù),利用它我們可以繪制各種各樣的圖形、小動(dòng)物。這個(gè)庫(kù)其實(shí)并不難,實(shí)際你怎么繪制這個(gè)圖形,對(duì)應(yīng)的代碼,就跟著你的實(shí)際繪制圖形的方向走下去,即可。

 
from turtle import *

# 繪制鼻子
def nose(x,y):
    penup()
    goto(x,y)
    pendown()
    setheading(-30)
    begin_fill()
    a=0.4
    for i in range(120):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) 
            forward(a) 
        else:
            a=a-0.08
            left(3)
            forward(a)
        end_fill()
        
    penup()
    setheading(90)
    forward(25)
    setheading(0)
    forward(10)
    pendown()
    pencolor(255,155,192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)
    end_fill()
    penup()
    setheading(0)
    forward(20)
    pendown()
    pencolor(255,155,192)
    setheading(10)
    begin_fill()
    circle(5)
    color(160,82,45)
    end_fill()
# 繪制頭部
def head(x,y):
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    setheading(0)
    pendown()
    begin_fill()
    setheading(180)
    circle(300,-30)
    circle(100,-60)
    circle(80,-100)
    circle(150,-20)
    circle(60,-95)
    setheading(161)
    circle(-300,15)
    penup()
    goto(-100,100)
    pendown()
    setheading(-30)
    a=0.4
    for i in range(60):
        if 0<=i<30 or 60<=i<90:
            a=a+0.08
            left(3) 
            forward(a) 
        else:
            a=a-0.08
            left(3)
            forward(a)
    end_fill()
# 繪制耳朵
def ears(x,y): 
    color((255,155,192),"pink")
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,54)
    end_fill()
    penup()
    setheading(90)
    forward(-12)
    setheading(0)
    forward(30)
    pendown()
    begin_fill()
    setheading(100)
    circle(-50,50)
    circle(-10,120)
    circle(-50,56)
    end_fill()
# 繪制眼睛
def eyes(x,y):
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-20)
    setheading(0)
    forward(-95)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
    color((255,155,192),"white")
    penup()
    setheading(90)
    forward(-25)
    setheading(0)
    forward(40)
    pendown()
    begin_fill()
    circle(15)
    end_fill()
    color("black")
    penup()
    setheading(90)
    forward(12)
    setheading(0)
    forward(-3)
    pendown()
    begin_fill()
    circle(3)
    end_fill()
# 繪制腮幫
def cheek(x,y):
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    begin_fill()
    circle(30)
    end_fill()
# 繪制嘴巴
def mouth(x,y): 
    color(239,69,19)
    penup()
    goto(x,y)
    pendown()
    setheading(-80)
    circle(30,40)
    circle(40,80)
# 繪制身體
def body(x,y):
    color("red",(255,99,71))
    penup()
    goto(x,y)
    pendown()
    begin_fill()
    setheading(-130)
    circle(100,10)
    circle(300,30)
    setheading(0)
    forward(230)
    setheading(90)
    circle(300,30)
    circle(100,3)
    color((255,155,192),(255,100,100))
    setheading(-135)
    circle(-80,63)
    circle(-150,24)
    end_fill()
# 繪制手
def hands(x,y):
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(-160)
    circle(300,15)
    penup()
    setheading(90)
    forward(15)
    setheading(0)
    forward(0)
    pendown()
    setheading(-10)
    circle(-20,90)
    penup()
    setheading(90)
    forward(30)
    setheading(0)
    forward(237)
    pendown()
    setheading(-20)
    circle(-300,15)
    penup()
    setheading(90)
    forward(20)
    setheading(0)
    forward(0)
    pendown()
    setheading(-170)
    circle(20,90)
# 繪制腳
def foot(x,y):
    pensize(10)
    color((240,128,128))
    penup()
    goto(x,y)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    color("black")
    pensize(15)
    forward(20)
    pensize(10)
    color((240,128,128))
    penup()
    setheading(90)
    forward(40)
    setheading(0)
    forward(90)
    pendown()
    setheading(-90)
    forward(40)
    setheading(-180)
    color("black")
    pensize(15)
    forward(20)
# 繪制尾巴
def tail(x,y):
    pensize(4)
    color((255,155,192))
    penup()
    goto(x,y)
    pendown()
    setheading(0)
    circle(70,20)
    circle(10,330)
    circle(70,30)
# 設(shè)置畫布和畫筆
def setting(): 
    pensize(4)
    hideturtle()
    colormode(255)
    color((255,155,192),"pink")
    setup(840,500)
    speed(10)
def main():
    setting()         # 畫布和畫筆設(shè)置
    nose(-100,100)    # 鼻子
    head(-69,167)     # 頭
    ears(0,160)       # 耳朵
    eyes(0,140)       # 眼睛
    cheek(80,10)      # 腮幫
    mouth(-20,30)     # 嘴巴
    body(-32,-8)      # 身體
    hands(-56,-45)    # 手
    foot(2,-177)      # 腳
    tail(148,-155)    # 尾巴
    done()            # 結(jié)束
if __name__ == '__main__':
    main()

結(jié)果如下:

2.利用python給小豬佩奇換背景色

換背景色的原理:每一個(gè)圖像都是由像素點(diǎn)構(gòu)成的,我們想要替換他們的顏色,就是找到每個(gè)像素點(diǎn)對(duì)應(yīng)的位置,然后用指定顏色,去替換它!一般證件照背景色并不是同一種藍(lán)色像素點(diǎn),無(wú)法完成像素點(diǎn)的定位,這就需要我們對(duì)圖像進(jìn)行【腐蝕】或【膨脹】,完成圖片黑白話,這樣白色的像素點(diǎn)是255,就可以很好的定位了。

 
import cv2
import numpy as np
# 讀取照片
img=cv2.imread('zhu.jpg')

# 圖像縮放
img = cv2.resize(img,None,fx=0.5,fy=0.5)
rows,cols,channels = img.shape
print(rows,cols,channels)
cv2.imshow('img',img)

# 圖片轉(zhuǎn)換為灰度圖
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
cv2.imshow('hsv',hsv)

# 圖片的二值化處理
lower_blue=np.array([90,70,70])
upper_blue=np.array([110,255,255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)


#腐蝕膨脹
erode=cv2.erode(mask,None,iterations=1)
cv2.imshow('erode',erode)

dilate=cv2.dilate(erode,None,iterations=1)
cv2.imshow('dilate',dilate)

#遍歷替換
for i in range(rows):
  for j in range(cols):
    if erode[i,j]==255: # 像素點(diǎn)為255表示的是白色,我們就是要將白色處的像素點(diǎn),替換為紅色
      img[i,j]=(0,0,255)#此處替換顏色,為BGR通道
cv2.imshow('res',img)

# 窗口等待的命令,0表示無(wú)限等待
cv2.waitKey(0)

結(jié)果如下:

怎么用python繪制一個(gè)小豬佩奇

3.利用python將小豬佩奇切分為九宮格

將圖片切分為九宮格的原理就是:找到圖片對(duì)應(yīng)位置的坐標(biāo),然后進(jìn)行切割。由于是九宮格,我們切分的是3*3,然后利用雙層循環(huán)遍歷對(duì)應(yīng)位置的坐標(biāo)后,進(jìn)行圖片切割

 
from PIL import Image
import sys
#將圖片填充為正方形
def fill_image(image):
    width, height = image.size
    #選取長(zhǎng)和寬中較大值作為新圖片的,小的地方,用圖片填充為等寬等高
    new_image_length = width if width > height else height
    #生成新圖片[白底]
    new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white')
    #將之前的圖粘貼在新圖上,居中
    if width > height:#原圖寬大于高,則填充圖片的豎直維度
        #(x,y)二元組表示粘貼上圖相對(duì)下圖的起始位置
        new_image.paste(image, (0, int((new_image_length - height) / 2)))
    else:
        new_image.paste(image, (int((new_image_length - width) / 2),0))
    return new_image
#切圖
def cut_image(image):
    width, height = image.size
    item_width = int(width / 3)
    item_height = int(height / 3)
    box_list = []
    #雙重循環(huán),生成9張圖片基于原圖的位置
    # 注意:圖片左上角是(0,0),右下角是(width,height)
    for i in range(0,3):
        for j in range(0,3):
            print((j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height))
            box = (j*item_width,i*item_height,(j+1)*item_width,(i+1)*item_height)
            box_list.append(box)
 
    image_list = [image.crop(box) for box in box_list]
    return image_list
#保存
def save_images(image_list):
    index = 1
    for image in image_list:
        image.save(str(index) + '.jpg')
        index += 1
 
file_path = "zhuzhu.jpg"
image = Image.open(file_path)
image = fill_image(image)
image_list = cut_image(image)
save_images(image_list)

結(jié)果如下:

4.利用python制作小豬佩奇動(dòng)態(tài)二維碼

代碼說(shuō)明:如果我們利用的背景圖是gif動(dòng)態(tài)圖,生成的就是動(dòng)態(tài)二維碼。如果利用的背景是靜態(tài)圖,生成的是靜態(tài)二維碼。

 
from MyQR import myqr


# 生成的二維碼最終在你電腦的存儲(chǔ)位置
# 當(dāng)你使用了動(dòng)態(tài)圖作為背景,這里可以寫成".gif",保存出來(lái)的就是gif動(dòng)態(tài)二維碼!
save_name = r'C:\Users\Administrator\Desktop\fdasfa\D.gif'
myqr.run(
    words='https://jq.qq.com/?_wv=1027&k=aKS84NJF',
    version=10,  # 容錯(cuò)率
    level='H',  # 糾錯(cuò)水平,范圍是L、M、Q、H,從左到右依次升高
    colorized=True, # False為黑白
    contrast=1.5,  # 用以調(diào)節(jié)圖片的對(duì)比度,1.0 表示原始圖片。
    brightness=1.0,  # 用來(lái)調(diào)節(jié)圖片的亮度。
    save_name=save_name, #存儲(chǔ)的文件名
    # 背景圖片的路徑,你如果給的是".png/.jpg"等靜態(tài)圖片,最終生成的就是靜態(tài)二維碼!
    # 背景圖片的路徑,你如果給的是".gif"等動(dòng)態(tài)圖片,最終只需要保存為".gif",生成的就是動(dòng)態(tài)二維碼!
    picture=r'C:\Users\Administrator\Desktop\fdasfa\123.gif'
    )

怎么用python繪制一個(gè)小豬佩奇

到此,關(guān)于“怎么用python繪制一個(gè)小豬佩奇”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

分享標(biāo)題:怎么用python繪制一個(gè)小豬佩奇
文章源于:http://muchs.cn/article48/iiophp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)建站公司、外貿(mào)建站、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司