七夕不懂浪漫?Python 帮你制造惊喜!!!

Python技术

共 8161字,需浏览 17分钟

 · 2021-08-16

文 | 潮汐

来源:Python 技术「ID: pythonall」

接天庭通知:今年七夕,由于牛郎织女没有接种新冠疫苗,没有核酸检测报告,暂时不能相见,故今年七夕取消,也请大家不要聚集,烛光晚餐在家吃也挺好,牛郎织女作表率,争做全网好市民。

所以作为程序员中的一员,怎么能不用技术给女朋友制造惊喜呢?

今天的文章是教各位朋友们如何用技术制造浪漫,如何给自己的女神或女朋友表白!!!

制作爱心发射小人


import turtle
import time
from turtle import mainloop, hideturtle


def clear_all():
    turtle.penup()
    turtle.goto(00)
    turtle.color('white')
    turtle.pensize(800)
    turtle.pendown()
    turtle.setheading(0)
    turtle.fd(300)
    turtle.bk(600)


# 重定位海龟的位置
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.74545)
    turtle.circle(size * -1.431165)
    turtle.left(120)
    turtle.circle(size * -1.431165)
    turtle.circle(size * -3.74545)
    turtle.fd(size)
    turtle.end_fill()


# 画出发射爱心的小人
def draw_people(x, y):
    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()
    turtle.pensize(2)
    turtle.color('black')
    turtle.setheading(0)
    turtle.circle(60360)
    turtle.penup()
    turtle.setheading(90)
    turtle.fd(75)
    turtle.setheading(180)
    turtle.fd(20)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(2360)
    turtle.setheading(0)
    turtle.penup()
    turtle.fd(40)
    turtle.pensize(4)
    turtle.pendown()
    turtle.circle(-2360)
    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(-3500)
    turtle.color(t_color)
    turtle.write(text, font=('宋体', font_size, 'normal'))
    time.sleep(show_time)
    clear_all()


# 爱心发射
def draw_():
    turtle.speed(0)
    draw_people(-25020)
    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(900500)

    draw_text("Are You Readly?""black"600)
    draw_text("接下来""skyblue"600)
    draw_text("感谢你的出现,让我的日子这么甜!""pink"353)
    draw_()
    # 使用mainloop防止窗口卡死
    mainloop()

if __name__ == '__main__':
    
    main()
    

效果图如下:

动态图需要请各位小主运行代码观看!

生成素描画

找一张女神或女朋友的照片画出素描画,用技术画出素描画,再打印出来给她留作纪念,代码和效果图如下:


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(0255)

im = Image.fromarray(b.astype('uint8'))  # 重构图像
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",
        )

效果图:

总结

望有情人终成眷属,且行且珍惜!!!

参考

作者丨二哥不像程序员

https://blog.csdn.net/u014779536/article/details/108418066

PS公号内回复「Python」即可进入Python 新手学习交流群,一起 100 天计划!


老规矩,兄弟们还记得么,右下角的 “在看” 点一下如果感觉文章内容不错的话,记得分享朋友圈让更多的人知道!

代码获取方式

识别文末二维码,回复:潮汐


浏览 11
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报