【Dataset】 Chardataset Generate

    科技2022-09-08  162

    1. 根据不同字体生成字符图片数据集

    from PIL import Image, ImageDraw, ImageFont, ImageFilter import random import matplotlib.pyplot as plt import numpy as np import os #数据集根目录 rootpath="./data/" #windows上字体文件路径 fontpath='C:/windows/fonts/' #图片旋转角度 rotateAngel=[-20,-15,-10,-5,5,10,15,20] #生成图片大小 width = 96 height = 96 #需要生成得字体字符 letters='abcdefghijklmnopqrstuvwxyzCFIJKLOPSUVWXYZ' CharData = [letters[i] for i in range(0,len(letters))] # 黑色,用户显示字体 def blackColor(): return (0,0,0) # 根据data数据生成目录文件,data=[a,b,c,d,] 这种字符数列 def mkdir_for_imgs(data): for tm in data: if os.path.isdir(rootpath+tm): pass else: print(rootpath+tm) os.makedirs(rootpath+tm) #罗列和手势分开字符比较相似的不同font 名称 def getFontList(fontpath): listfontnames=os.listdir(fontpath) return [name[:-4] for name in listfontnames] #电脑上总共有663个字体返回的有效字体199个 ['50416___.TTF', '65659___.TTF', '75678___.TTF', '75749___.TTF', 'AGENCYR.TTF', 'ANTQUAB.TTF', 'ANTQUABI.TTF', 'ANTQUAI.TTF', 'arial.ttf', 'ariali.ttf', 'ARIALN.TTF', 'ARIALNB.TTF', 'ARIALNBI.TTF', 'ARIALNI.TTF', 'ARIALUNI.TTF', 'bahnschrift.ttf', 'BASKVILL.TTF', 'BELL.TTF', 'BELLB.TTF', 'BELLI.TTF', 'BKANT.TTF', 'BOD_B.TTF', 'BOD_BI.TTF', 'BOD_CI.TTF', 'BOD_I.TTF', 'BOD_R.TTF', 'BOOKOS.TTF', 'BOOKOSI.TTF', 'BRADHITC.TTF', 'calibri.ttf', 'calibrib.ttf', 'calibrii.ttf', 'calibril.ttf', 'calibrili.ttf', 'CALIFI.TTF', 'CALIFR.TTF', 'CALIST.TTF', 'CALISTB.TTF', 'CALISTBI.TTF', 'CALISTI.TTF', 'cambriai.ttf', 'Candara.ttf', 'Candarab.ttf', 'Candarai.ttf', 'Candaral.ttf', 'Candarali.ttf', 'Candaraz.ttf', 'CENSCBK.TTF', 'CENTAUR.TTF', 'CENTURY.TTF', 'comic.ttf', 'comici.ttf', 'comicz.ttf', 'consola.ttf', 'consolab.ttf', 'consolai.ttf', 'consolaz.ttf', 'constan.ttf', 'constanb.ttf', 'constani.ttf', 'corbel.ttf', 'corbelb.ttf', 'corbeli.ttf', 'corbell.ttf', 'corbelli.ttf', 'cour.ttf', 'courbd.ttf', 'courbi.ttf', 'couri.ttf', 'Deng.ttf', 'Dengb.ttf', 'Dengl.ttf', 'DUBAI-LIGHT.TTF', 'DUBAI-MEDIUM.TTF', 'DUBAI-REGULAR.TTF', 'ebrima.ttf', 'ebrimabd.ttf', 'ERASDEMI.TTF', 'ERASLGHT.TTF', 'ERASMD.TTF', 'FRABK.TTF', 'FRABKIT.TTF', 'FRADMCN.TTF', 'FRADMIT.TTF', 'FRAMDCN.TTF', 'framdit.ttf', 'FREESCPT.TTF', 'FTLTLT.TTF', 'FZSTK.TTF', 'FZYTK.TTF', 'Gabriola.ttf', 'GARA.TTF', 'GARABD.TTF', 'GARAIT.TTF', 'georgia.ttf', 'georgiai.ttf', 'GILC____.TTF', 'GILI____.TTF', 'GIL_____.TTF', 'GLECB.TTF', 'GOTHICI.TTF', 'GOUDOS.TTF', 'GOUDOSI.TTF', 'himalaya.ttf', 'HTOWERT.TTF', 'HTOWERTI.TTF', 'INFROMAN.TTF', 'Inkfree.ttf', 'LBRITED.TTF', 'LBRITEDI.TTF', 'LBRITEI.TTF', 'LEELAWAD.TTF', 'LEELAWDB.TTF', 'LeelawUI.ttf', 'LeelUIsl.ttf', 'LFAX.TTF', 'LFAXD.TTF', 'LFAXI.TTF', 'LSANSDI.TTF', 'LSANSI.TTF', 'LTYPE.TTF', 'LTYPEB.TTF', 'LTYPEBO.TTF', 'LTYPEO.TTF', 'lucon.ttf', 'MAIAN.TTF', 'malgun.ttf', 'malgunbd.ttf', 'malgunsl.ttf', 'mingliub.ttc', 'mmrtext.ttf', 'MOD20.TTF', 'monbaiti.ttf', 'msgothic.ttc', 'msjh.ttc', 'msjhl.ttc', 'MSUIGHUB.TTF', 'MSUIGHUR.TTF', 'msyhl.ttc', 'msyi.ttf', 'Nirmala.ttf', 'NirmalaS.ttf', 'ntailu.ttf', 'OCRAEXT.TTF', 'palabi.ttf', 'palai.ttf', 'PERI____.TTF', 'PER_____.TTF', 'phagspa.ttf', 'REFSAN.TTF', 'ROCKI.TTF', 'SCHLBKI.TTF', 'segoeuii.ttf', 'segoeuil.ttf', 'segoeuisl.ttf', 'seguiemj.ttf', 'seguihis.ttf', 'seguili.ttf', 'seguisb.ttf', 'seguisbi.ttf', 'seguisli.ttf', 'seguisym.ttf', 'simfang.ttf', 'simhei.ttf', 'simkai.ttf', 'SIMLI.TTF', 'simsun.ttc', 'simsunb.ttf', 'SIMYOU.TTF', 'Sitka.ttc', 'SitkaI.ttc', 'STFANGSO.TTF', 'STKAITI.TTF', 'STSONG.TTF', 'STXINWEI.TTF', 'STZHONGS.TTF', 'sylfaen.ttf', 'tahoma.ttf', 'taile.ttf', 'TCCM____.TTF', 'TCMI____.TTF', 'TCM_____.TTF', 'times.ttf', 'timesbi.ttf', 'timesi.ttf', 'trebucit.ttf', 'tt0142m_.ttf', 'tt0143m_.ttf', 'tt0200m_.ttf', 'tt0372m_.ttf', 'tt0395m_.ttf', 'tt0849m_.ttf', 'TT1139M_.TTF', 'tt2002m_.ttf', 'verdana.ttf', 'verdanai.ttf', 'YuGothL.ttc', 'YuGothM.ttc', 'YuGothR.ttc'] #fontname: 字体得名称 # chardata: 需要生成图片的字符 # picturename: 生成图片的名字 #outputdir: 生成图片存储目录 # 字符居中,生成单张图片 def generateBlackPicuture(fontname,chardata,picturename,outputdir): try: fullfontpath=os.path.join(fontpath,fontname) # 创建Image image = Image.new('RGB', (width, height), (255, 255, 255)) #选择字体 font = ImageFont.truetype(fullfontpath, 80) # 创建Font对象: # 创建Draw对象: draw = ImageDraw.Draw(image) #设置字体居中对齐 imwidth, imheight = image.size font_width, font_height = draw.textsize(chardata, font) draw.text(((imwidth - font_width-font.getoffset(chardata)[0]) / 2, (imheight - font_height-font.getoffset(chardata)[1]) / 2),chardata,font=font,fill=blackColor()) #image = image.filter(ImageFilter.BLUR) image.save(os.path.join(outputdir,picturename+'.png')) #进行旋转操作并进行处理 rotateImage(outputdir,picturename+'.png',outputdir) # 模糊: # image = image.filter(ImageFilter.BLUR) except Exception as err: pass #图片旋转操作 #inputdir: the inputdir of dataset #filename: the name of picture to handle # outputdir: the name of outputdir to save rotated file def rotateImage(inputdir,filename,outputdir): # original image img = Image.open(os.path.join(inputdir,filename)) # converted to have an alpha layer im2 = img.convert('RGBA') for i in range(0,24,8): savepath=os.path.join(outputdir,filename+str(i) +'.png') # rotated image rot = im2.rotate(i, expand=1) # a white image same size as rotated image fff = Image.new('RGBA', rot.size, (255,)*4) # create a composite image using the alpha layer of rot as a mask out = Image.composite(rot, fff, rot) out=out.resize((96,96),Image.ANTIALIAS) # save your work (converting back to mode='1' or whatever..) out.convert(img.mode).save(savepath) for i in range(336,360,8): savepath=os.path.join(outputdir,filename+str(i)+'.png') # rotated image rot = im2.rotate(i, expand=1) # a white image same size as rotated image fff = Image.new('RGBA', rot.size, (255,)*4) # create a composite image using the alpha layer of rot as a mask out = Image.composite(rot, fff, rot) out=out.resize((96,96),Image.ANTIALIAS) # save your work (converting back to mode='1' or whatever..) out.convert(img.mode).save(savepath) #生成单个字符数据集 # charname: 字符名称 #charnamedir: 字符数据集存储对应的文件夹目录 def generateSingleCharDataset(charname,charnamedir): for fonttemp in getFontList("./png"): generateBlackPicuture(fonttemp,charname,fonttemp.split('.')[0],charnamedir) #生成所有字符数据集 def generateAlldata(): mkdir_for_imgs(CharData) for charname in CharData: generateSingleCharDataset(charname,os.path.join(rootpath,charname)) if __name__ == "__main__": generateAlldata()

     

    Processed: 0.013, SQL: 9