class D():
import os
import cv2
from xpinyin import Pinyin
import re
import base64
import shutil
import time
li=[]
bhjx_path=''
ppbs_path=''
jjjg_path=''
jzgl_path=''
temp=[]
def __init__(self):
pass
def get_jison_file(self,file_path=r'C:\Users\lenovo\Desktop\ddj-汉字',save_path=r'C:\Users\lenovo\Desktop\ddj-汉字.txt'):
self.file_path = file_path
self.save_path = save_path
for i in self.__class__.os.listdir(self.file_path):
i=self.__class__.os.path.join(self.file_path,i)
self.__class__.li.append(i)
self.__class__.bhjx_path = self.__class__.li[0]
self.__class__.ppbs_path = self.__class__.li[1]
self.__class__.jjjg_path = self.__class__.li[2]
self.__class__.jzgl_path = self.__class__.li[3]
# bh
for i in self.__class__.os.listdir(self.__class__.bhjx_path):
s = {}
s['bh']=i[5:]
s['df'] =self.__class__.Pinyin().get_pinyin(i[5:],'')
self.__class__.temp.append(s)
bh=str(self.__class__.temp).replace("'bh':","bh:").replace("'df':","df:").replace(' ','')
print(bh)
with open(self.save_path,mode='a+',encoding='utf-8') as f:
f.write(bh+'\n'+'\n')
self.__class__.temp=[]
# ppbs
for i in self.__class__.os.listdir(self.__class__.ppbs_path):
s = {}
s['ppbs']=i[5:]
s['df'] =self.__class__.Pinyin().get_pinyin(i[5:],'')
self.__class__.temp.append(s)
ppbs=str(self.__class__.temp).replace("'ppbs':","ppbs:").replace("'df':","df:").replace(' ','')
print(ppbs)
with open(self.save_path,mode='a+',encoding='utf-8') as f:
f.write(ppbs+'\n'+'\n')
self.__class__.temp = []
# jjjg
for i in self.__class__.os.listdir(self.__class__.jjjg_path):
s = {}
s['jjjg']=i[5:]
s['df'] =self.__class__.Pinyin().get_pinyin(i[5:],'')
self.__class__.temp.append(s)
jjjg=str(self.__class__.temp).replace("'jjjg':","jjjg:").replace("'df':","df:").replace(' ','')
print(jjjg)
with open(self.save_path,mode='a+',encoding='utf-8') as f:
f.write(jjjg+'\n'+'\n')
self.__class__.temp = []
# jzgl
for i in self.__class__.os.listdir(self.__class__.jzgl_path):
s = {}
s['jzgl']=i[5:]
s['df'] =self.__class__.Pinyin().get_pinyin(i[5:],'')
self.__class__.temp.append(s)
jzgl=str(self.__class__.temp).replace("'jzgl':","jzgl:").replace("'df':","df:").replace(' ','')
print(jzgl)
with open(self.save_path,mode='a+',encoding='utf-8') as f:
f.write(jzgl)
self.__class__.temp = []
def creat_svg(self,png_path,img_path,svg_path,model_path=r'C:\Users\lenovo\Desktop\模板.svg'):
self.png_path=png_path
self.img_path=img_path
self.save_path=svg_path
self.model_path=model_path
with open(self.model_path, mode='rb') as f:
m=f.read()
s = m.decode('utf-8')
goal=self.__class__.re.findall('base64,(.*?)"', s, self.__class__.re.S)
with open(self.img_path,mode='rb') as f:
imgdata1 = self.__class__.base64.b64encode(f.read())
# print(str(imgdata1)[2:-1])
with open( self.png_path,mode='rb') as f:
imgdata2 = self.__class__.base64.b64encode(f.read())
# print(str(imgdata2)[2:-1])
newsvg=s.replace(goal[1],str(imgdata2)[2:-1]).replace(goal[0],str(imgdata1)[2:-1]).encode('utf-8')
with open(self.save_path,mode='wb') as f:
f.write(newsvg)
print(self.save_path+' ok')
def file_chinese_to_pinyin(self,file_path):
self.file_path = file_path
self.__class__.base_path = self.file_path
for i in self.__class__.os.listdir(self.file_path):
i = self.__class__.os.path.join(self.file_path, i)
self.__class__.li.append(i)
for i in self.__class__.li:
for k in self.__class__.os.listdir(i):
nowname =self.__class__.os.path.join(i,self.__class__.Pinyin().get_pinyin(k,''))
oldname =self.__class__.os.path.join(i,k)
self.__class__.os.rename(oldname,nowname)
def replace_all_svg(self,file_path,svg_path=r'C:\Users\lenovo\Desktop\ddj-done',imgtype='.jpg'):
self.file_path = file_path
self.svg_path=svg_path
self.img_path=imgtype
goalpath=[]
for i in self.__class__.os.walk(self.file_path):
if i[1]==[]:
for k in i[2]:
goalpath.append(self.__class__.os.path.join(i[0],k))
for i in goalpath:
sv=['.svg',]
for v in sv:
path = self.__class__.os.path.join(self.svg_path,self.__class__.os.path.basename(i).replace(self.img_path, v))
if self.__class__.os.path.isfile(path):
print(path)
with open(path,mode='rb') as f:
s=f.read()
with open(i.replace(self.img_path,v),mode='wb') as f:
f.write(s)
else:
pass
def insert_png_to_svg(self,svg_path,png_path):
self.svg_path=svg_path
self.png_path=png_path
with open(self.png_path, mode='rb') as f:
goal = str(self.__class__.base64.b64encode(f.read()))[2:-1]
with open(self.svg_path, mode='rb+') as f:
s=f.read().decode('utf-8')
goal=f'<image\n y="-0.0019368411"\n x="0.0082369847"\n id="image887"\n xlink:href="data:image/png;base64,{goal}"\n style="display:none;image-rendering:optimizeSpeed"\n preserveAspectRatio="none"\n height="1024"\n width="1024" />\n</svg>\n'
s=str(s).replace('</svg>',goal).encode('utf-8')
with open(self.svg_path, mode='wb') as f:
f.write(s)
print(self.svg_path+' png替换ok')
def make_dir(self,li,img_save_file=r'C:\Users\lenovo\Desktop\图片预设'):
self.li=li
self.save_path=img_save_file
for i in li:
print('当前在 '+i+'文件夹')
self.__class__.os.makedirs(rf'C:\Users\lenovo\Desktop\书法字帖\制作\{i}')
s=0
while 1:
img_move_file = self.__class__.os.listdir(self.save_path)
while img_move_file==[]:
self.__class__.time.sleep(0.1)
img_move_file = self.__class__.os.listdir(self.save_path)
if img_move_file==[]:
# print('Please add picture to goal directory,My master')
pass
img_move_path = self.__class__.os.path.join(self.save_path, img_move_file[0])
path = self.__class__.os.path.join(rf'C:\Users\lenovo\Desktop\书法字帖\制作\{i}', str(s) + '.png')
if img_move_file[0].startswith('1'):
with open(img_move_path, mode='rb') as f:
key = f.read()
path=self.__class__.os.path.join(rf'C:\Users\lenovo\Desktop\书法字帖\制作\{i}',str(s)+'.png')
with open(path, mode='wb') as f:
f.write(key)
self.__class__.os.remove(img_move_path)
break
else:
with open(img_move_path, mode='rb') as f:
key = f.read()
path=self.__class__.os.path.join(rf'C:\Users\lenovo\Desktop\书法字帖\制作\{i}',str(s)+'.png')
with open(path, mode='wb') as f:
f.write(key)
s = s + 1
self.__class__.os.remove(img_move_path)
print(i+'finish')
def check_svg(self,svg_path=r'C:\Users\lenovo\Desktop\cslsjx-svg-done'):
self.svg_path=svg_path
k=0
for i in self.__class__.os.listdir(self.svg_path):
with open(self.__class__.os.path.join(self.svg_path,i),mode='rb')as f:
s=str(f.read())
if 'id="outline"' in s and 'id="writebrush"' in s:
pass
else:
print(i)
k=k+1
print(k)
def video_img(self,video_path,v=['fp','.jpg']):
for p in [ i for i in self.__class__.os.walk(video_path)]:
if p[1]==[] and p[0].endswith(v[0]):
for s in p[2]:
path=self.__class__.os.path.join(p[0],s)
video_capture =self.__class__.cv2.VideoCapture(path)
frameNum = 0
while 1:
frameNum = frameNum + 1
ret, frame = video_capture.read()
fps = video_capture.get(5)
if frameNum == video_capture.get(7):
print(path)
print(video_capture.get(7))
self.__class__.cv2.imwrite(path.replace('.mp4',v[1]).replace(v[0],'img'), frame)
break
video_capture.release()
def video_rename(self,imgpath):
li=self.__class__.os.listdir(imgpath)
for i in li:
self.__class__.os.rename(self.__class__.os.path.join(imgpath,i),self.__class__.os.path.join(imgpath,i))
def change_svg(self,img_path,svg_path):
self.img_path=img_path
self.save_path=svg_path
with open(self.svg_path, mode='rb') as f:
m=f.read()
s = m.decode('utf-8')
goal=self.__class__.re.findall('base64,(.*?)"', s, self.__class__.re.S)
with open(self.img_path,mode='rb') as f:
imgdata1 = self.__class__.base64.b64encode(f.read())
# print(str(imgdata1)[2:-1])
with open( self.png_path,mode='rb') as f:
imgdata2 = self.__class__.base64.b64encode(f.read())
# print(str(imgdata2)[2:-1])
newsvg=s.replace(goal[1],str(imgdata2)[2:-1]).replace(goal[0],str(imgdata1)[2:-1]).encode('utf-8')
with open(self.save_path,mode='wb') as f:
f.write(newsvg)
print(self.save_path+' ok')
if __name__ == '__main__':
a = D()