# coding:utf-8
import json
import os
import random
import re
import requests
import time
import urllib.parse
from string import punctuation
import cv2
from PIL import ImageFont, ImageDraw, Image
import numpy as np
# 编辑图片路径
def save_pic(title1, title2, bk_img_dir, save_dir):
"""
保存图片
:param title1: 标题1
:param title2:标题2
:param bk_img_dir: 背景图片位置
:param save_dir: 保存位置
:return:
"""
bk_img = cv2.imread(bk_img_dir)
img_pil = Image.fromarray(bk_img)
long, high = img_pil.size
result1_list = re.findall('[a-zA-Z0-9]+', title1)
result1_len = len(''.join(result1_list))/2 if len(''.join(result1_list)) else 0
title1_len = len(title1)- result1_len
result2_list = re.findall('[a-zA-Z0-9]+', title2)
result2_len = len(''.join(result2_list))/2 if len(''.join(result2_list)) else 0
title2_len = len(title2) - result2_len
# f_size = int(long / (max([len(title1), len(title2)]) ))
f_size = int(long / (max([title1_len+2, title2_len+2]) ))
if f_size >= 60 :
f_size = 60
# 设置需要显示的字体
fontpath = "font/msyh.ttc"
# 为字体大小
font = ImageFont.truetype(fontpath,size=f_size)
draw = ImageDraw.Draw(img_pil)
r1 = int((long - title1_len * f_size) / 2)
r2 = int((long - title2_len * f_size) / 2.3)
h1 = int((high - f_size - f_size - f_size) / 2)
h2 = int(h1 + 2 * f_size)
fillColor = "orange" # 设置字体颜色
fillColor2 = "white" # 设置字体颜色
draw.text((r1, h1), title1, font=font, fill=fillColor)
draw.text((r2, h2), title2, font=font, fill=fillColor2)
bk_img = np.array(img_pil)
# 保存图片路径
# cv2.imshow("add_text", bk_img)
# cv2.waitKey()
b = bk_img_dir.split('.')[-1]
nonce = ''.join(random.sample('0123456789abcdefghijklmnopqrstuvwxyz', 6))
save_img = os.path.join(save_dir,str(int(time.time()*1000))+nonce+'.'+b)
cv2.imwrite(save_img, bk_img)
return '
'%(save_img,title1,title2)
def get_data(f_dir, dir,bk_pic_dir,save_pic_dir):
world = f_dir.replace('.txt', '')
world = world.replace('\xe2\x80\x8b', '')
world = world.replace(' ', '')
dicts = {i: '' for i in punctuation}
punc_table = str.maketrans(dicts)
world = world.translate(punc_table)
world = world.replace('(', '')
world = world.replace(')', '')
world = world.replace('?', '')
world = world.replace('?', '')
world = world.replace(':', '')
world = world.replace(':', '')
world = world.replace('!', '')
world = world.replace('!', '')
world = world.replace('“', '')
world = world.replace('"', '')
world = world.replace('”', '')
world = world.replace('-', '')
world = world.replace('——', '')
world = world.replace(',', '')
world = world.replace(',', '')
world = world.replace('|', '')
world = world.replace('、', '')
world = world.replace('(', '')
world = world.replace(')', '')
world = world.strip()
world = remove_upprintable_chars(world)
world = world[0:20]
if '_' not in f_dir.replace('.txt', '') and not f_dir.replace('.txt', '').endswith(')'):
#if ')' not in world:
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (
world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('数据不足,尝试1', world)
world = world[0:15]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('--尝试10个字--', world)
world = world[0:10]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('--尝试9个字--', world)
world = world[0:9]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('--尝试8个字--', world)
world = world[0:8]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('--尝试7个字--', world)
world = world[0:7]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
if 'g' not in data or len(data['g']) <= 2:
print('--尝试6个字--', world)
world = world[0:6]
url = 'https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=%s&csor=16&pwd=%s' % (world, world)
c = requests.get(url)
data = c.json()
return
c = data['g']
w_list = random.sample(c, 3)
rstr = "[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |'
#print (w_list[0]['q'])
#print (w_list[1]['q'])
#print (w_list)
title = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]').sub('', w_list[0]['q'])
title = re.sub(rstr, "", title) # 替换为下划线
title2 = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]').sub('', w_list[1]['q'])
title2 = re.sub(rstr, "", title2) # 替换为下划线
title3 = re.compile('[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]').sub('', w_list[2]['q'])
title3 = re.sub(rstr, "", title3) # 替换为下划线
# 随机挑选一张图片
bk_pic = random.choice([i for i in os.listdir(bk_pic_dir) if i.endswith('.png') or i.endswith('.jpg')])
pic_dir = os.path.join(bk_pic_dir,bk_pic)
# title2 = f_dir.split('.')[0]
pic = save_pic(title,title2,pic_dir,save_pic_dir)
# 保存图片
with open(os.path.join(dir, f_dir), 'r+', encoding='utf-8') as f:
try: content = f.read()
except:
return
#finally:
# print('++++++++++')
f.seek(0, 0)
f.write('
' + random.choice(['为您提取本文阅读重点:','为您推荐文章知识点:','通过这篇文章,您可以了解到这些问题:','文章阅读关键词:','文章原标题:','文章内容核心观点提取:','本文知识点归纳及重点:','本文阅读重点有下面几个方向:','这篇文章知识点为您总结归纳如下:','文章划重点,请关注以下方面:','本文核心要素如下:','为您提供本文核心观点如下:']) + '%s、%s、%s ' % (
f_dir.replace('.txt', ''), w_list[1]['q'], w_list[0]['q']) + '
\n\n %s \n ' % (pic) + content) # 内容头部插入
# f.write('%s,%s,%s'%(w_list[0]['q'],w_list[1]['q'],w_list[2]['q']) + content)
if len(f_dir) <= 30:
#nwe_name = "%s_" % title + f_dir.replace('.txt', '') + '.txt' #下拉标题+原标题
nwe_name = "%s(%s)" % (title ,title3) + '.txt' #下拉标题1+下拉标题3
print(f_dir.replace('.txt', '') + '->' + nwe_name)
os.rename(os.path.join(dir, f_dir), os.path.join(dir, nwe_name))
def start(files_dir,bk_pic_dir,save_pic_dir):
for f in os.listdir(files_dir):
if f.endswith('.txt'):
# try:
get_data(f, files_dir,bk_pic_dir,save_pic_dir)
# except Exception as e:
# print('接口调取失败,等待5秒 %s' % str(e))
# time.sleep(5)
def remove_upprintable_chars(s):
return ''.join(x for x in s if x.isprintable())
if __name__ == '__main__':
# files_dir = r'C:\Users\Administrator\Desktop\tigerok\txt'
files_dir = os.path.abspath('')
# bk_pic_dir = r'D:\picku\gongsibaidiheizi' #白色遮罩
bk_pic_dir = r'D:\picku\gongsiheidibaizi' #黑色遮罩
save_pic_dir = r'C:\Users\qs\Desktop\tigerok\pic'
start(files_dir,bk_pic_dir,save_pic_dir)
# print()