import pandas as pd
from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
import cv2
from PIL import ImageDraw, ImageFont
from PIL._imaging import fontimport csv
import codecs
#两个函数用于保存需要的坐标点
# def save_to(path,name):
# # file_csv =open(path,"a+",newline='',encoding="utf-8")
# file_csv = codecs.open(path, 'a+', 'utf-8') # 追加
# # writer=csv.writer(file_csv)
# writer = csv.writer(file_csv, delimiter=',', quotechar=',', quoting=csv.QUOTE_MINIMAL)
# for data in name:
# writer.writerow(data)def save_to(name,data):f = open(name,"a+",newline='',encoding='utf-8')csv_writer = csv.writer(f)csv_writer.writerow(data)f.close()data=pd.read_csv('E:\qichacha\data\qichacha.csv',encoding='gbk')
#字体大小
fnt = ImageFont.truetype(r'E:\qichacha\gen_data\WeiRuanYaHei-1.ttf', 18)
fnt2 = ImageFont.truetype(r'E:\qichacha\gen_data\WeiRuanYaHei-1.ttf', 22)
fnt3 = ImageFont.truetype(r'E:\qichacha\gen_data\WeiRuanYaHei-1.ttf', 24)
fnt4 = ImageFont.truetype(r'E:\qichacha\gen_data\WeiRuanYaHei-1.ttf', 20)
# 按下列方式计算其余坐标
# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]
for i in range(0,len(data)-1):im = Image.open('E:\qichacha\gen_data\model.jpg')draw = ImageDraw.Draw(im)#draw.text(左上坐标, 书写内容, fill='颜色', font=字体)draw.text((637, 566), data.iloc[i]['信用代码'], fill='black', font=fnt) # 信用代码w, h = fnt.getsize(data.iloc[i]['信用代码'])# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]a=[637,566,637+w,566,637,566+h,637+w,566+h,data.iloc[i]['信用代码']]#保存两种格式为后面使用方便save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)loc1 = len(data.iloc[i]['登记机关'])for k in range(0,int(loc1/8)+1):#字体超过8个字,就换行try:draw.text((660, 1222+k*30), data.iloc[i]['登记机关'][k*8:k*8+8], fill='black', font=fnt3) # 登记机关w, h = fnt3.getsize(data.iloc[i]['登记机关'][k*8:k*8+8])# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]a = [660,1222+(k*30),660+w,1222+(k*30),660, 1222+(k*30)+h, 660+w,1222+(k*30)+h,data.iloc[i]['登记机关'][k*8:k*8+8]]save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)except:passif k==4:#超过5行就不显示,可以调整breakloc2 = len(data.iloc[i]['注册地址'])for j,m in enumerate(['企业名称','类型','注册地址','法定代表人','注册资本','成立日期','营业期限']):# print(m)if m=="注册地址":#防止字数超过图片本身draw.text((330, 635 + j * 40), data.iloc[i][m][0:30], fill='black', font=fnt2)w, h = fnt2.getsize(data.iloc[i][m][0:30])# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]a = [330, 635 + (j * 40), 330 + w, 635 + (j * 40), 330, 635 + (j * 40) + h, 330 + w, 635 + (j * 40) + h,data.iloc[i][m]]save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)else:draw.text((330, 635+j*40),data.iloc[i][m], fill='black', font=fnt2) # 企业名称,类型,注册地址,法定代表人,注册资本,成立日期,营业期限w, h = fnt2.getsize(data.iloc[i][m])# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]a = [330, 635+(j*40), 330 + w, 635+(j*40), 330, 635 + (j*40) + h, 330 + w, 635 + (j*40)+ h,data.iloc[i][m]]save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)loc = len(data.iloc[i]['经营范围'])for n in range(0,int(loc/30)+1):try:draw.text((330, 920 +n*30),data.iloc[i]['经营范围'][n*30:n*30+30], fill='black', font=fnt4) # 经营范围w, h = fnt4.getsize(data.iloc[i]['经营范围'][n*30:n*30+30])# [宽,高,宽+w,高,宽,高+h,宽+w,高+h]a = [330, 920 +(n*30), 330 + w, 920 +(n*30), 330, 920 + (n*30) + h, 330 + w, 920 + (n*30) + h,data.iloc[i]['经营范围'][n * 30:n * 30 + 30]]save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)except:passif n==5:draw.text((330, 920 + (n+1) * 30), u'.............', fill='black', font=fnt4)#多余6行就写省略号w, h = fnt4.getsize(".............")a = [330, 920 +((n+1)*30), 330 + w, 920 + ((n+1)*30), 330, 920 + ((n+1)*30)+ h, 330 + w, 920 + ((n+1)*30) + h,"............."]save_to("E:\qichacha\data\zuobiao\\txt\{}.txt".format(i), a)save_to("E:\qichacha\data\zuobiao\\csv\{}.csv".format(i), a)breakprint("成功--{}".format(data.iloc[i]['企业名称']))im.save('E:\qichacha\img\yingye\{}.jpg'.format(data.loc[i]['信用代码']))#保存import os
import cv2
import numpy as np
n=0
a=0
#图片的融合。保存用循环数字。
path = r"E:\qichacha\background"
path1=r"E:\qichacha\img\yingye"
for i in os.listdir(path):#生成的营业执照路径back = os.path.join(path, i)image = cv2.imread(back)k = 0for j in os.listdir(path1):#营业执照back1= os.path.join(path1, j)logo = cv2.imread(back1)# num = (logo.width, logo.height)height, width = logo.shape[0:2]#获取宽高#图片按宽高缩放cv2.resize(图片, 缩放大小)image_1=cv2.resize(image, (width, height))logo_1=cv2.resize(logo, (width, height))#融合图片cv2.addWeighted(主图、背景图, 权重, 辅图, 权重, 0)image_copy = cv2.addWeighted(logo_1, 0.8, image_1, 0.2, 0)cv2.imwrite("E:\qichacha\img\\beijing\{}.jpg".format(a), image_copy)# cv2.waitKey(0)# cv2.destroyAllWindows()print("成功--{}{}".format(data.iloc[k]['企业名称'],n))k += 1a+=1n+=1
#保存图片,用信用代码,前面加0,1,2区分不同的噪声
# n=0
# path = r"E:\qichacha\background"
# path1=r"E:\qichacha\img\yingye"
# for i in os.listdir(path):#背景图
# back = os.path.join(path, i)
# image = cv2.imread(back)
# k = 0
# for j in os.listdir(path1):#营业执照
# back1= os.path.join(path1, j)
# logo = cv2.imread(back1)
# # num = (logo.width, logo.height)
# height, width = logo.shape[0:2]
# image_1=cv2.resize(image, (width, height))
# logo_1=cv2.resize(logo, (width, height))
# image_copy = cv2.addWeighted(logo_1, 0.6, image_1, 0.4, 0)
# cv2.imencode('.jpg', image_copy)[1].tofile("E:\qichacha\img\\beijing\{}{}".format(n,j))
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# print("成功--{}{}".format(n,j))
# k += 1
# n+=1#单张图片的融合
# image=cv2.imread(r'E:\qichacha\background\\1.jpg')#背景图
# logo = cv2.imread(r'E:\qichacha\img\yingye\\91110000MA00GXCN9M.jpg')#生成的营业执照
# # 图像融合
# height,width=logo.shape[0:2]
# # print(width,height)
# # a=cv2.resize(image, (width, height))
# image_1=cv2.resize(image, (width, height))
# logo_1=cv2.resize(logo, (width, height))
# combine = cv2.addWeighted(logo_1, 0.9, image_1, 0.1,0)
# cv2.imshow('s',combine)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# cv2.imwrite("E:\qichacha\img\\beijing\j2.jpg", combine)#保存英文名字
# cv2.imencode('.jpg', image_copy)[1].tofile("E:\qichacha\img\\beijing\{}{}.jpg".format(data.iloc[k]['企业名称'], n))#保存中文名#单张营业执照的生成
# im = Image.open('E:\qichacha\gen_data\model.jpg')
# draw = ImageDraw.Draw(im)
# draw.text((330,633), data.iloc[118,1], fill='black', font=fnt2)#企业名称
# w, h = fnt2.getsize(data.iloc[118,1])#宽高
# print(w,h)
# draw.text((330,675), data.iloc[118,6], fill='black', font=fnt2)#企业类型
# draw.text((330,715), data.iloc[118,9], fill='black', font=fnt2)#注册地址
# draw.text((330,755), data.iloc[118,2], fill='black', font=fnt2)#法定代表人
# draw.text((330,800), data.iloc[118,4], fill='black', font=fnt2)#注册资本
# draw.text((330,840), data.iloc[118,3], fill='black', font=fnt2)#成立日期
# draw.text((330,880), data.iloc[118,7], fill='black', font=fnt2)#营业期限# loc=len(data.iloc[118,10])
# for i in range(0,int(loc/30)):
# try:
# draw.text((330,920+i*30), data.iloc[117,10][i*30:i*30+31], fill='black', font=fnt4)#经营范围
# except:
# pass
# if i==5:
# draw.text((330, 920 + (i+1) * 30), u'.............', fill='black', font=fnt4)
# break
#
# draw.text((637,566), data.iloc[118,0], fill='black', font=fnt)#信用代码
# draw.text((650,1222), data.iloc[118,8], fill='black', font=fnt3)#登记机关
# im.show()# image=Image.open(r'E:\qichacha\background\1.jpg')#背景图
# logo = Image.open(r'E:\qichacha\img\营业执照\安徽淘宝村实业集团有限公司.jpg')#生成的营业执照
# num=(image.width,image.height)
# logo.thumbnail(num)#缩小营业执照,背景图大小
# image_copy=image.copy()#复制图像背景图
# x = int(random.randint(0, (image_copy.width- logo.width)+ 1))#随机生成背景图和营业执照宽的差值之间的数值
# y= int(random.randint(0, (image_copy.height - logo.height) + 1))#随机生成背景图和营业执照高的差值之间的数值
# image_copy.paste(logo,(x,y)) #将营业执照放到指定粘贴位置(左上角坐标)
# resize(temImage, dstImage2, Size(temImage.cols * 2, temImage.rows * 2), 0, 0, INTER_LINEAR)
# plt.imshow(image_copy)
# plt.show()
# image_copy.save('E:\qichacha\img\背景营业执照\j2.jpg')#保存
数据:csv文件:https://download.csdn.net/download/yangzheng_520/21979543
模板:https://download.csdn.net/download/yangzheng_520/21979571
不行的按照前面文章自己爬取