我这儿有个相似的例程,你可以参考下:
效果图,在图片上添加文字:
python3代码:
import os
import random
import time
from PIL import Image, ImageFont, ImageDraw
# 选择图片
img_list = os.listdir("./wallpaper/")
img_name = "./wallpaper/" + random.choice(img_list)
# 打开图片
img = Image.open(img_name)
im = img.transpose(Image.ROTATE_90) #旋转90°
draw = ImageDraw.Draw(im)
# 计算日期区域RGB均值
count = 0
R, G, B = 0, 0, 0
for width in range(im.size[0] // 2 - 300, im.size[0] // 2 + 340):
for high in range(im.size[1] // 2 - 90, im.size[1] // 2 + 80):
R += im.getpixel((width, high))[0]
G += im.getpixel((width, high))[1]
B += im.getpixel((width, high))[2]
count += 1
R = 255 - (R // count)
G = 255 - (G // count)
B = 255 - (B // count)
# 在图片上添加日期
write_string = "{}年{}月{}日\n" \
" News".format(time.localtime().tm_year,time.localtime().tm_mon,time.localtime().tm_mday)
newfont = ImageFont.truetype('simkai.ttf', 120)
draw.text((im.size[0] // 2 - 440, im.size[1] // 2 - 100), write_string, (R, G, B), font=newfont)
# 计算来源区域RGB均值
count = 0
R, G, B = 0, 0, 0
for width in range(im.size[0] //2 - 200, im.size[0]):
for high in range(im.size[1] - 180, im.size[1]):
R += im.getpixel((width, high))[0]
G += im.getpixel((width, high))[1]
B += im.getpixel((width, high))[2]
count += 1
if (R+G+B)//3//count>128:
pixel = 0
else:
pixel = 255
R = 255 - (R // count)
G = 255 - (G // count)
B = 255 - (B // count)
# 在图片上添加来源
resouce_string = "背景出处:小米壁纸"
newfont = ImageFont.truetype('simkai.ttf', 30)
draw.text((im.size[0]//2 - 150, im.size[1] - 180), resouce_string, (pixel, pixel, pixel), font=newfont)
# 另存为名称
save_name = "C:/Users/ZKer/Desktop/news/wallpaper_{}.jpg".format(int(time.time()))
im.save(save_name)
其中将数据以表格形式写入图片参考:
或者使用matplotlib:
或者是使用webkit2png模块,将数据做成网页,然后导出为图片。这个我没有经验,请自行搜索。
另外一个不靠谱的思路:截图,截取屏幕然后可以裁剪之类的
Linux&Windows:
import os
import platform
from time import time
def SC():
im_name = "{}.{}".format(int(time()), "png")
im = None
if platform.system() == 'Windows' or platform.system() == 'Darwin':
try:
from PIL import ImageGrab
im = ImageGrab.grab()
except OSError as e:
print('截图错误')
return
elif platform.system() == 'Linux':
try:
import pyscreenshot as ImageGrab
im = ImageGrab.grab()
except OSError as e:
print('截图错误')
return
im.save(im_name)