自作パンチカード

コードにナンセンスはありません

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。壁紙を取得します。サイズは携帯電話にぴったりです。ここでは、クローラーが取得したホタテの最初の画面を直接使用し、ローカルに書き込み、2つのパラメーター、コンテンツ、翻訳を返します
。2。次にPILを使用します。上記の関数を受け取るライブラリ2つの戻り値、テキスト処理、描画、2次元コードの貼り付け、保存
ヒント:PILライブラリの色構成はRGBではなくBGRです。注意してください着色するときに正しい入力に。

このチェックインの重要性は、読み取り、チェックアウト、および実行に使用できるすべてのカスタマイズにあります。任意の配置と変更は問題ありません。
指示:
ここに画像の説明を挿入

効果画像:
ここに画像の説明を挿入
関数を使用する場合は、関数に日数パラメーターを追加するだけで済みます

おすすめ

転載: blog.csdn.net/qq_17802895/article/details/112057340