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

學無先后,達者為師

網站首頁 編程語言 正文

Python+Turtle制作獨特的表白圖_python

作者:Python?集中營 ? 更新時間: 2022-06-23 編程語言

開始之前先來看看效果圖,在控制臺輸入相應的參數設置即可生成自己獨特的表白圖。

file

想要在圖片上書寫什么樣的信息,就看你的發揮了,哈哈哈~

file

import turtle as tle  # 小烏龜繪圖庫

使用turtle小烏龜畫圖之前,先進行全局參數初始化的設置,并使得全局初始化函global_init可以動態傳參供后面的方便調用。

def global_init(w=900, h=600, psize=15, sp=0):
    '''
    全局畫筆屬性初始化函數
    :param w: 畫布寬度
    :param h: 畫布高度
    :param psize: 畫筆尺寸
    :param sp: 繪畫速度
    :return:
    '''
    tle.setup(width=w, height=h)
    tle.color('red', 'pink')
    tle.pensize(psize)
    tle.speed(sp)

設置完上面的屬性之后,就可以正式開始繪圖了,這里為了使代碼結構清晰還是創建一個表白紅心的函數draw_red_heart。

def draw_red_heart(confession='我愛你', confession_h=30,
                   sign='來自遠方的仰慕者', sign_w=20, sign_h=25):
    '''
    繪制表白紅心
    :param confession: 表白語句
    :param confession_h: 表白語句尺寸大小
    :param sign: 簽名
    :param sign_w: 簽名寬度
    :param sign_h: 簽名高度
    :return:
    '''
    tle.up()
    tle.hideturtle()
    tle.goto(0, -180)
    tle.showturtle()
    tle.down()
    tle.speed(500)
    tle.begin_fill()
    tle.left(140)
    tle.forward(224)
    for n in range(200):
        tle.right(1)
        tle.forward(2)
    tle.left(120)
    for n in range(200):
        tle.right(1)
        tle.forward(2)
    tle.forward(224)
    tle.end_fill()
    tle.pensize(12)
    tle.up()
    tle.hideturtle()
    tle.goto(0, -20)
    tle.showturtle()
    tle.color('#CD5C5C', 'pink')
    tle.write(confession, font=('gungsuh', confession_h), align="center")
    tle.up()
    tle.hideturtle()
    tle.color('black', 'pink')
    tle.goto(180, -180)
    tle.showturtle()
    tle.write(sign, font=(sign_w, sign_h), align="center", move=True)

編寫主體業務函數main,調用整個繪畫程序運行。

def main():
    print('按要求輸入下面的繪圖參數...')
    width = int(input('請輸入畫布寬度:\n'))
    height = int(input('請輸入畫布高度:\n'))
    psize = int(input('請輸入畫筆尺寸:\n'))
    speed = int(input('請輸入畫筆速度:\n'))
    confession = input('請輸入表白語句:\n')
    confession_h = int(input('請輸入表白語句字體大小:\n'))
    sign = input('請輸入簽名:\n')
    sign_w = int(input('請輸入簽名寬度:\n'))
    sign_h = int(input('請輸入簽名高度:\n'))
    print('參數錄入完成,開始繪圖...')
    global_init(w=width, h=height, psize=psize, sp=speed)
    draw_red_heart(confession=confession, confession_h=confession_h,
                   sign=sign, sign_w=sign_w, sign_h=sign_h)
    print('繪圖完成!')
    # 保存eps格式的繪圖結果
    eps = tle.getscreen()
    eps.getcanvas().postscript(file=r"./表白.eps")

    # 保存JPG格式的圖片
    from PIL import Image
    im = Image.open("./表白.eps")
    im.save("表白.jpg")
    print('繪圖保存成功,默認在當前路徑!')

    # 繪圖完成后保持窗口不被關閉
    screen = tle.Screen()
    screen.exitonclick()

注意:在將表白.eps文件保存為JPG格式的文件時會拋出如下的錯誤。

raise OSError("Unable to locate Ghostscript on paths")

這個時候不要著急,當然有解決的辦法的,到ghostscript官網下載自己需要的組件就OK了。

file

然后,將下載好的gs9561w64.exe可執行應用安裝就成了。

file

記得安裝完成后,路徑都是在這個地方,不需要也可以隨時卸載。

file

最后,將該路徑加入到環境變量的path屬性中就大功告成啦,重啟一下開發工具這樣環境變量就會真正的生效了。

file

再次提醒,記得配置完環境變量后重啟IDE喔!

main()

原文鏈接:https://www.cnblogs.com/lwsbc/p/16187397.html

欄目分類
最近更新