溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么用python繪制一個小豬佩奇

發(fā)布時間:2021-11-25 14:38:14 來源:億速云 閱讀:239 作者:iii 欄目:大數(shù)據(jù)

這篇文章主要介紹“怎么用python繪制一個小豬佩奇”,在日常操作中,相信很多人在怎么用python繪制一個小豬佩奇問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用python繪制一個小豬佩奇”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

1.利用python繪制一個小豬佩奇

turtle庫是一個很好的python圖形繪制庫,利用它我們可以繪制各種各樣的圖形、小動物。這個庫其實并不難,實際你怎么繪制這個圖形,對應的代碼,就跟著你的實際繪制圖形的方向走下去,即可。

 
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)
# 設置畫布和畫筆
def setting(): 
    pensize(4)
    hideturtle()
    colormode(255)
    color((255,155,192),"pink")
    setup(840,500)
    speed(10)
def main():
    setting()         # 畫布和畫筆設置
    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()            # 結束
if __name__ == '__main__':
    main()

結果如下:

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

換背景色的原理:每一個圖像都是由像素點構成的,我們想要替換他們的顏色,就是找到每個像素點對應的位置,然后用指定顏色,去替換它!一般證件照背景色并不是同一種藍色像素點,無法完成像素點的定位,這就需要我們對圖像進行【腐蝕】或【膨脹】,完成圖片黑白話,這樣白色的像素點是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: # 像素點為255表示的是白色,我們就是要將白色處的像素點,替換為紅色
      img[i,j]=(0,0,255)#此處替換顏色,為BGR通道
cv2.imshow('res',img)

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

結果如下:

怎么用python繪制一個小豬佩奇

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

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

 
from PIL import Image
import sys
#將圖片填充為正方形
def fill_image(image):
    width, height = image.size
    #選取長和寬中較大值作為新圖片的,小的地方,用圖片填充為等寬等高
    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)二元組表示粘貼上圖相對下圖的起始位置
        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)

結果如下:

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

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

 
from MyQR import myqr


# 生成的二維碼最終在你電腦的存儲位置
# 當你使用了動態(tài)圖作為背景,這里可以寫成".gif",保存出來的就是gif動態(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,  # 容錯率
    level='H',  # 糾錯水平,范圍是L、M、Q、H,從左到右依次升高
    colorized=True, # False為黑白
    contrast=1.5,  # 用以調(diào)節(jié)圖片的對比度,1.0 表示原始圖片。
    brightness=1.0,  # 用來調(diào)節(jié)圖片的亮度。
    save_name=save_name, #存儲的文件名
    # 背景圖片的路徑,你如果給的是".png/.jpg"等靜態(tài)圖片,最終生成的就是靜態(tài)二維碼!
    # 背景圖片的路徑,你如果給的是".gif"等動態(tài)圖片,最終只需要保存為".gif",生成的就是動態(tài)二維碼!
    picture=r'C:\Users\Administrator\Desktop\fdasfa\123.gif'
    )

怎么用python繪制一個小豬佩奇

到此,關于“怎么用python繪制一個小豬佩奇”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

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

AI