自制打卡

不废话 上代码

import cv2,requests
from PIL import ImageFont, ImageDraw, Image
import numpy as np
import os


def parse():
    url = 'https://rest.shanbay.com/api/v2/quote/quotes/today/'
    res = requests.get(url)
    # print(res.json())
    img = res.json()['data']['origin_img_urls'][0]
    content = res.json()['data']['content']
    translation = res.json()['data']['translation']
    image = requests.get(img).content
    with open('C:/Users/Administrator/Desktop/wallpaper.jpg','wb')as file:
        file.write(image)
    return content,translation
def image(num):
    s1,s2 = parse()
    # 编辑图片路径
    bk_img = cv2.imread("C:/Users/Administrator/Desktop/wallpaper.jpg")
    # 设置需要显示的字体
    fontpath = "font/simsun.ttc"
    # 32为字体大小
    font = ImageFont.truetype(fontpath, 50)
    img_pil = Image.fromarray(bk_img)
    draw = ImageDraw.Draw(img_pil)
    # 绘制文字信息
    # (100,300/350)为字体的位置,(255,255,255)为白色,(0,0,0)为黑色
    # -------------------字符处理--------------------------
    # s1 = "Tomorrow is the first blank page of a 365 page book. Write a good one."
    str2 = list(s1)
    str2.insert(42,"\n")
    text1= "".join(str2)

    # s2 = "新年之书有365张空白页,明天是第一页。书写美好的明天吧!"
    str5 = list(s2)
    str5.insert(22,"\n")
    text2= "".join(str5)



    #pil中的颜色是BGR而不是RGB
    draw.text((350, 200), "坚持打卡天数", font=ImageFont.truetype(fontpath, 70), fill=(193,182,255),stroke_width = 1)
    draw.text((460, 290), "%2d天"%(num), font=ImageFont.truetype(fontpath, 70), fill=(193,182,255),stroke_width = 1)

    draw.text((0, 1700), text1, font=font, fill=(255, 255, 255))
    # y轴间隔100坐标,50个坐标换一行
    draw.text((0, 1800), text2, font=font, fill=(255, 255, 255))

    draw.text((900, 1650), "加好友一起背单词", font=ImageFont.truetype(fontpath, 22), fill=(255,255,255))



    img1 = Image.open("C:/Users/Administrator/Desktop/imgserver.jpg")
    img_pil.paste(img1,(900,1450))


    bk_img = np.array(img_pil)


    # 保存图片路径
    cv2.imwrite("C:/Users/Administrator/Desktop/new_img.jpg", bk_img)
    os.system("C:/Users/Administrator/Desktop/new_img.jpg")
    # os.system(r'del C:\Users\Administrator\Desktop\new_img.jpg')



if __name__=="__main__":
    image(20)

原理:
1.获取一张壁纸,尺寸要跟手机刚刚好的,这里我直接使用爬虫获取的扇贝第一屏,写入本地,返回两个参数,content,translation
2.然后使用PIL库接收上面函数的两个返回值,然后进行导入,文本处理,绘图,贴上二维码,进行保存
tip:在PIL库中颜色组成是BGR而并非RGB,在进行上色时候注意输入正确。

这个打卡的意义在于一切自定义,用来读书打卡,跑步都行,任意编排,改型都是没问题的。
使用方法:
在这里插入图片描述

效果图:
在这里插入图片描述
在使用的时候只需要在函数添加天数参数即可

猜你喜欢

转载自blog.csdn.net/qq_17802895/article/details/112057340