七夕不懂浪漫?Python 幫你制造驚喜!!!

文 | 潮汐
來源:Python 技術(shù)「ID: pythonall」

接天庭通知:今年七夕,由于牛郎織女沒有接種新冠疫苗,沒有核酸檢測報告,暫時不能相見,故今年七夕取消,也請大家不要聚集,燭光晚餐在家吃也挺好,牛郎織女作表率,爭做全網(wǎng)好市民。
所以作為程序員中的一員,怎么能不用技術(shù)給女朋友制造驚喜呢?
今天的文章是教各位朋友們?nèi)绾斡眉夹g(shù)制造浪漫,如何給自己的女神或女朋友表白!!!
制作愛心發(fā)射小人
import turtle
import time
from turtle import mainloop, hideturtle
def clear_all():
turtle.penup()
turtle.goto(0, 0)
turtle.color('white')
turtle.pensize(800)
turtle.pendown()
turtle.setheading(0)
turtle.fd(300)
turtle.bk(600)
# 重定位海龜?shù)奈恢?/span>
def go_to(x, y, state):
turtle.pendown() if state else turtle.penup()
turtle.goto(x, y)
def draw_heart(size):
turtle.color('red', 'pink')
turtle.pensize(2)
turtle.pendown()
turtle.setheading(150)
turtle.begin_fill()
turtle.fd(size)
turtle.circle(size * -3.745, 45)
turtle.circle(size * -1.431, 165)
turtle.left(120)
turtle.circle(size * -1.431, 165)
turtle.circle(size * -3.745, 45)
turtle.fd(size)
turtle.end_fill()
# 畫出發(fā)射愛心的小人
def draw_people(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.pensize(2)
turtle.color('black')
turtle.setheading(0)
turtle.circle(60, 360)
turtle.penup()
turtle.setheading(90)
turtle.fd(75)
turtle.setheading(180)
turtle.fd(20)
turtle.pensize(4)
turtle.pendown()
turtle.circle(2, 360)
turtle.setheading(0)
turtle.penup()
turtle.fd(40)
turtle.pensize(4)
turtle.pendown()
turtle.circle(-2, 360)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(20)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(40)
turtle.setheading(0)
turtle.fd(35)
turtle.setheading(-60)
turtle.fd(10)
turtle.penup()
turtle.goto(x, y)
turtle.setheading(-90)
turtle.pendown()
turtle.fd(60)
turtle.setheading(-135)
turtle.fd(60)
turtle.bk(60)
turtle.setheading(-45)
turtle.fd(30)
turtle.setheading(-135)
turtle.fd(35)
turtle.penup()
# 繪制文字
def draw_text(text, t_color, font_size, show_time):
turtle.penup()
turtle.goto(-350, 0)
turtle.color(t_color)
turtle.write(text, font=('宋體', font_size, 'normal'))
time.sleep(show_time)
clear_all()
# 愛心發(fā)射
def draw_():
turtle.speed(0)
draw_people(-250, 20)
turtle.penup()
turtle.goto(-150, -30)
draw_heart(14)
turtle.penup()
turtle.goto(-200, -200)
turtle.color('pink')
turtle.write('Biu~', font=('宋體', 60, 'normal'))
turtle.penup()
turtle.goto(-20, -60)
draw_heart(25)
turtle.penup()
turtle.goto(-70, -200)
turtle.color('pink')
turtle.write('Biu~', font=('宋體', 60, 'normal'))
turtle.penup()
turtle.goto(200, -100)
draw_heart(45)
turtle.penup()
turtle.goto(150, -200)
turtle.color('pink')
turtle.write('Biu~', font=('宋體', 60, 'normal'))
turtle.hideturtle()
time.sleep(3)
def main():
# 隱藏海龜
hideturtle()
turtle.setup(900, 500)
draw_text("Are You Readly?", "black", 60, 0)
draw_text("接下來", "skyblue", 60, 0)
draw_text("感謝你的出現(xiàn),讓我的日子這么甜!", "pink", 35, 3)
draw_()
# 使用mainloop防止窗口卡死
mainloop()
if __name__ == '__main__':
main()
效果圖如下:

動態(tài)圖需要請各位小主運行代碼觀看!
生成素描畫
找一張女神或女朋友的照片畫出素描畫,用技術(shù)畫出素描畫,再打印出來給她留作紀念,代碼和效果圖如下:
from PIL import Image
import numpy as np
a = np.asarray(Image.open(r".\wife.jpg").convert('L')).astype('float')
depth = 10. # (0-100)
grad = np.gradient(a) # 取圖像灰度的梯度值
grad_x, grad_y = grad # 分別取橫縱圖像梯度值
grad_x = grad_x * depth / 100.
grad_y = grad_y * depth / 100.
A = np.sqrt(grad_x ** 2 + grad_y ** 2 + 1.)
uni_x = grad_x / A
uni_y = grad_y / A
uni_z = 1. / A
vec_el = np.pi / 2.2 # 光源的俯視角度,弧度值
vec_az = np.pi / 4. # 光源的方位角度,弧度值
dx = np.cos(vec_el) * np.cos(vec_az) # 光源對x 軸的影響
dy = np.cos(vec_el) * np.sin(vec_az) # 光源對y 軸的影響
dz = np.sin(vec_el) # 光源對z 軸的影響
b = 255 * (dx * uni_x + dy * uni_y + dz * uni_z) # 光源歸一化
b = b.clip(0, 255)
im = Image.fromarray(b.astype('uint8')) # 重構(gòu)圖像
im.save(r".\result.jpg")
print("保存成功,請查看")
效果圖如下:

生成專屬二維碼
最后一個大驚喜,用 Python 簡單的給女朋友或者女神制作專屬二維碼,代碼和效果圖如下:
from MyQR import myqr
myqr.run(words="Welcome to Here!",
version=6,
picture="wife.jpg",
colorized=True,
save_name="ewm.png",
)
效果圖:
總結(jié)
望有情人終成眷屬,且行且珍惜!!!
參考
作者丨二哥不像程序員
https://blog.csdn.net/u014779536/article/details/108418066
PS:公號內(nèi)回復(fù)「Python」即可進入Python 新手學習交流群,一起 100 天計劃!
老規(guī)矩,兄弟們還記得么,右下角的 “在看” 點一下,如果感覺文章內(nèi)容不錯的話,記得分享朋友圈讓更多的人知道!


【代碼獲取方式】
評論
圖片
表情
