1.将图片进行base64编码
import os
import sys
from base64 import b64encode
workdir = os.path.abspath(".")
image_name = sys.argv[-1]
image_path = os.path.join(workdir, image_name)
with open(file=image_path, mode="rb") as image_fp, open(file=f"{image_name}.txt", mode="wt", encoding="UTF-8") as txt_fp:
image = image_fp.read()
txt_fp.write(b64encode(image).decode("UTF-8"))
2. python convert.py bg.jpg
3.将目录下生成的txt中的编码复制到vscode css文件
body { background-size: cover; // 背景大小。 background-repeat: no-repeat; // 图片是否重复(重叠),这里设为不重叠。 background-position: center; // 背景图片位置,这里设为居中。 opacity: 0.78; // 背景透明度,数值越小,背景图片越鲜明。 background-image: url("data:image/jpg;base64, [这里粘贴已编码的图片]) // 图片地址。 }