时光荏苒,转眼2019就要逝去……哎呀,算了好文艺,不太适合我
切入正题,话说2019年底到2020年春节,有圣诞,元旦,春节,情人节 这四个重要的节日,各位程序员小哥哥们都是如何表白心仪的妹子呢?送花?送礼物?送吃的?陪逛街陪吃饭配电影陪……NO NO NO……这些别的臭男人也能想到的。
我们作为 “年二十有三,头秃,始从文,连考而不中。遂习武,练武场上发一矢,中鼓吏,逐之出。改学IT,自撰一函数,用之,堆栈溢出” 的程序猿,肯定要跟别的男的不一样啦!别人想得到的,我们也能想到,别人想不到的,我们“一款游戏一包烟,一台电脑一下午。一盒泡面一壶水,一顿能管一整天。”也要想到,因为啥??
ok废话不多说,现在我就送上程序猿们的必杀技。
如何在圣诞来临之际,通过程序去表白。
程序运行过程如下:
运行环境:python3.7
# -*- coding:utf-8 -*-
from urllib.request import urlopen
import win32gui, win32con, win32api, os, shutil, winreg
import time
from PIL import Image, ImageDraw, ImageFont
def set_desktop_pic(ipath):
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, ipath, 1 + 2)
def get_desktop_pic():
k = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Control Panel\\Desktop")
img, _ = winreg.QueryValueEx(k, "WallPaper")
winreg.CloseKey(k)
return img
# 下载需要设置的桌面背景图
def download_img(img_url, img_name):
try:
data = urlopen(img_url).read()
with open(img_name, 'wb') as img:
img.write(data)
ipath = img_name
except Exception as err:
print('使用默认桌面', err)
ipath = get_desktop_pic()
return os.path.abspath(ipath)
# 文字展示
def show_2019():
ipath = get_desktop_pic()
cpath = os.path.abspath('tmp' + '_' + os.path.basename(ipath))
shutil.copyfile(ipath, cpath)
img = Image.open(cpath)
img_size = img.size
mx, my = img_size[0], img_size[1]
fs = 128
txt = '2 0 1 8,亲 爱 的 MTbaby ,圣诞 + 元旦,节 日 快 乐,虫 儿 飞 祝'
tabs = txt.split(',')
rs = max([len(v) for v in tabs]) * fs
y = fs
left = (mx-rs)/2
for row in tabs:
x = left
for col in row:
cpath = add_pic_text(cpath, x, y, col, txt_rgb=(255, 0, 0), font_size=fs)
set_desktop_pic(cpath)
time.sleep(0.2)
x += fs
print(col, end='')
y += fs + fs/5
time.sleep(0.5)
print('')
# 再桌面背景上添加表白文字
def set_desktop_pic_text(ipath, txt):
cpath = os.path.abspath('tmp' + '_' + os.path.basename(ipath))
shutil.copyfile(ipath, cpath)
# 图片大小
img = Image.open(cpath)
img_size = img.size
mx, my = img_size[0], img_size[1]
tabs = txt.split(',')
fs = 48
rs = max([len(v) for v in tabs]) * fs
y = fs
for row in tabs:
x = mx - rs - fs
for col in row:
cpath = add_pic_text(cpath, x, y, col)
set_desktop_pic(cpath)
time.sleep(0.2)
x += fs
print(col, end='')
y += fs + 10
time.sleep(0.5)
print('')
def add_pic_text(ipath, x, y, txt, txt_rgb=(255, 255, 255), font_size=48):
try:
img = Image.open(ipath)
font = ImageFont.truetype('simsun.ttc', font_size)
draw = ImageDraw.Draw(img)
draw.text((x, y), txt, font=font, fill=txt_rgb)
img.save(ipath, 'jpeg')
except Exception as err:
print('添加文本失败:', err)
return ipath
def download_text():
a = ''
a += '\u62e9\u4e00\u57ce\u7ec8\u8001\uff0c\u9047\u4e00\u4eba\u767d\u9996\uff0c\u633d\u4e00\u5e18\u5e7d'
a += '\u68a6\uff0c\u62e9\u4e00\u4eba\u6df1\u7231\uff0c\u7559\u4e00\u4e16\u7e41\u534e\uff0c\u5171\u5ea6'
a += '\u671d\u5915\u3002\uff0c\u611f\u8c22\u4e00\u76f4\u6709\u4f60\uff0c\u5b9d\u5b9d\u0028\u005e\u005f'
a += '\u002d\u0029\uff0c\u5723\u8bde\u8282\u5feb\u4e50\uff0c\u002d\u002d\u866b\u513f\u98de'
return str(a).strip().replace(' ', '')
if __name__ == '__main__':
# 1
show_2019()
# 2
img_url = 'https://img.zcool.cn/community/01cbb5585b6ffea8012060c8d899ae.jpg@1280w_1l_2o_100sh.jpg'
img_path = download_img(img_url, 'chr_img.jpg')
text = download_text()
set_desktop_pic_text(img_path, text)
创作不易,喜欢的话给喵喵赏点吧~(可怜眼神)