小編給大家分享一下Python+OpenCv如何制作證件圖片生成器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),蓋州企業(yè)網(wǎng)站建設(shè),蓋州品牌網(wǎng)站建設(shè),網(wǎng)站定制,蓋州網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,蓋州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。本項(xiàng)目使用Python和OpenCv實(shí)現(xiàn)身份證圖片生成工具,填入信息,選擇一張頭像圖片(即可生成黑白和彩色身份證圖片)??梢赃x擇是否自動(dòng)摳圖,自動(dòng)摳圖目前僅支持藍(lán)色背景,對(duì)自動(dòng)摳圖效果不滿意可以手動(dòng)摳圖。
參照標(biāo)準(zhǔn):
正面
“姓名”、“性別”、“民族”、“出生年月日”、“住址”、“公民身份號(hào)碼”為6號(hào)黑體字,用藍(lán)色油墨印刷;登記項(xiàng)目中的姓名項(xiàng)用5號(hào)黑體字印刷;其他項(xiàng)目則用小5號(hào)黑體字印刷;出生年月日 方正黑體簡(jiǎn)體字符大?。盒彰?hào)碼(11點(diǎn))其他(9點(diǎn))字符間距(AV):號(hào)碼(50)字符行距:住址(12點(diǎn));身份證號(hào)碼字體 OCR-B 10 BT 文字 華文細(xì)黑。
背面
左上角為國(guó)徽,用紅色油墨印刷;其右側(cè)為證件名稱“中華人民共和國(guó)居民身份證”,分上下兩排排列,其中上排的“中華人民共和國(guó)”為4號(hào)宋體字,下排的“居民身份證”為2號(hào)宋體字;“簽發(fā)機(jī)關(guān)”、“有效期限”為6號(hào)加粗黑體字;簽發(fā)機(jī)關(guān)登記項(xiàng)采用,“xx市公安局”;有效期限采用“xxxx.xx-xxxx.xx.xx”格式,使用5號(hào)黑體字印刷,全部用黑色油墨印刷。
這里我用周杰倫的圖像制作簡(jiǎn)單的身份證圖片,效果圖如下:
實(shí)現(xiàn)Demo如下:
# coding:utf-8 import os import PIL.Image as PImage from PIL import ImageFont, ImageDraw import cv2 import numpy as np try: from Tkinter import * from ttk import * from tkFileDialog import * from tkMessageBox import * except ImportError: from tkinter import * from tkinter.ttk import * from tkinter.filedialog import * from tkinter.messagebox import * if getattr(sys, 'frozen', None): base_dir = os.path.join(sys._MEIPASS, 'usedres') else: base_dir = os.path.join(os.path.dirname(__file__), 'usedres') def changeBackground(img, img_back, zoom_size, center): # 縮放 img = cv2.resize(img, zoom_size) rows, cols, channels = img.shape # 轉(zhuǎn)換hsv hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 獲取mask lower_blue = np.array([78, 43, 46]) upper_blue = np.array([110, 255, 255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) # cv2.imshow('Mask', mask) # 腐蝕膨脹 erode = cv2.erode(mask, None, iterations=1) dilate = cv2.dilate(erode, None, iterations=1) # 粘貼 for i in range(rows): for j in range(cols): if dilate[i, j] == 0: # 0代表黑色的點(diǎn) img_back[center[0] + i, center[1] + j] = img[i, j] # 此處替換顏色,為BGR通道 return img_back def paste(avatar, bg, zoom_size, center): avatar = cv2.resize(avatar, zoom_size) rows, cols, channels = avatar.shape for i in range(rows): for j in range(cols): bg[center[0] + i, center[1] + j] = avatar[i, j] return bg def generator(): global ename, esex, enation, eyear, emon, eday, eaddr, eidn, eorg, elife, ebgvar name = ename.get() sex = esex.get() nation = enation.get() year = eyear.get() mon = emon.get() day = eday.get() org = eorg.get() life = elife.get() addr = eaddr.get() idn = eidn.get() fname = askopenfilename(parent=root, initialdir=os.getcwd(), title=u'選擇頭像') # print fname im = PImage.open(os.path.join(base_dir, 'empty.png')) avatar = PImage.open(fname) # 500x670 name_font = ImageFont.truetype(os.path.join(base_dir, 'hei.ttf'), 72) other_font = ImageFont.truetype(os.path.join(base_dir, 'hei.ttf'), 60) bdate_font = ImageFont.truetype(os.path.join(base_dir, 'fzhei.ttf'), 60) id_font = ImageFont.truetype(os.path.join(base_dir, 'ocrb10bt.ttf'), 72) draw = ImageDraw.Draw(im) draw.text((630, 690), name, fill=(0, 0, 0), font=name_font) draw.text((630, 840), sex, fill=(0, 0, 0), font=other_font) draw.text((1030, 840), nation, fill=(0, 0, 0), font=other_font) draw.text((630, 980), year, fill=(0, 0, 0), font=bdate_font) draw.text((950, 980), mon, fill=(0, 0, 0), font=bdate_font) draw.text((1150, 980), day, fill=(0, 0, 0), font=bdate_font) start = 0 loc = 1120 while start + 11 < len(addr): draw.text((630, loc), addr[start:start + 11], fill=(0, 0, 0), font=other_font) start += 11 loc += 100 draw.text((630, loc), addr[start:], fill=(0, 0, 0), font=other_font) draw.text((950, 1475), idn, fill=(0, 0, 0), font=id_font) draw.text((1050, 2750), org, fill=(0, 0, 0), font=other_font) draw.text((1050, 2895), life, fill=(0, 0, 0), font=other_font) avatar = cv2.cvtColor(np.asarray(avatar), cv2.COLOR_RGB2BGR) im = cv2.cvtColor(np.asarray(im), cv2.COLOR_RGB2BGR) if ebgvar.get(): im = changeBackground(avatar, im, (500, 670), (690, 1500)) else: #im.paste(avatar, (1500, 690), mask=avatar) im = paste(avatar, im, (500, 670), (690, 1500)) im = PImage.fromarray(cv2.cvtColor(im, cv2.COLOR_BGR2RGB)) im.save('color.png') im.convert('L').save('bw.png') showinfo(u'成功', u'文件已生成到目錄下,黑白bw.png和彩色color.png') if __name__ == '__main__': global ename, esex, enation, eyear, emon, eday, eaddr, eidn, eorg, elife, ebgvar root = Tk() root.title(u'AIRobot身份證圖片生成器') # root.geometry('640x480') root.resizable(width=False, height=False) Label(root, text=u'姓名:').grid(row=0, column=0, sticky=W, padx=3, pady=3) ename = Entry(root, width=8) ename.grid(row=0, column=1, sticky=W, padx=3, pady=3) Label(root, text=u'性別:').grid(row=0, column=2, sticky=W, padx=3, pady=3) esex = Entry(root, width=8) esex.grid(row=0, column=3, sticky=W, padx=3, pady=3) Label(root, text=u'民族:').grid(row=0, column=4, sticky=W, padx=3, pady=3) enation = Entry(root, width=8) enation.grid(row=0, column=5, sticky=W, padx=3, pady=3) Label(root, text=u'出生年:').grid(row=1, column=0, sticky=W, padx=3, pady=3) eyear = Entry(root, width=8) eyear.grid(row=1, column=1, sticky=W, padx=3, pady=3) Label(root, text=u'月:').grid(row=1, column=2, sticky=W, padx=3, pady=3) emon = Entry(root, width=8) emon.grid(row=1, column=3, sticky=W, padx=3, pady=3) Label(root, text=u'日:').grid(row=1, column=4, sticky=W, padx=3, pady=3) eday = Entry(root, width=8) eday.grid(row=1, column=5, sticky=W, padx=3, pady=3) Label(root, text=u'住址:').grid(row=2, column=0, sticky=W, padx=3, pady=3) eaddr = Entry(root, width=32) eaddr.grid(row=2, column=1, sticky=W, padx=3, pady=3, columnspan=5) Label(root, text=u'證件號(hào)碼:').grid(row=3, column=0, sticky=W, padx=3, pady=3) eidn = Entry(root, width=32) eidn.grid(row=3, column=1, sticky=W, padx=3, pady=3, columnspan=5) Label(root, text=u'簽發(fā)機(jī)關(guān):').grid(row=4, column=0, sticky=W, padx=3, pady=3) eorg = Entry(root, width=32) eorg.grid(row=4, column=1, sticky=W, padx=3, pady=3, columnspan=5) Label(root, text=u'有效期限:').grid(row=5, column=0, sticky=W, padx=3, pady=3) elife = Entry(root, width=32) elife.grid(row=5, column=1, sticky=W, padx=3, pady=3, columnspan=5) Label(root, text=u'選項(xiàng):').grid(row=6, column=0, sticky=W, padx=3, pady=3) ebgvar = IntVar() ebg = Checkbutton(root, text=u'自動(dòng)摳圖', variable=ebgvar) ebg.grid(row=6, column=1, sticky=W, padx=3, pady=3, columnspan=5) Button(root, text=u'生成', width=32, command=generator).grid(row=7, column=1, sticky=W, padx=3, pady=3, columnspan=4) # root.iconbitmap(os.path.join(base_dir, 'ico.ico')) root.mainloop()
之前上傳代碼使用項(xiàng)目命名:“Python+OpenCv制作身份證圖片生成器代碼”,額、但是官方宣布違規(guī)禁止上傳,所示將項(xiàng)目名稱修改的不是那么明顯,感興趣的小伙伴可以下載運(yùn)行:
http://xiazai.jb51.net/201908/yuanma/xiangmu_code_jb51.rar
當(dāng)然為了方便操作,也可以使用pyinstaller模塊自己打包成應(yīng)用程序,首先需要使用命令安裝pyinstaller模塊:
pip install pyinstaller
Mac打包(打包成Mac app尚有問題未解決)
pyinstaller -i usedres/ico.icns --windowed --clean --noconfirm --onefile --add-data ./usedres:./usedres idcardgenerator.py
Windows打包
pyinstaller -i usedres/ico.ico --windowed --clean --noconfirm --onefile --add-data usedres;usedres idcardgenerator.py
以上是“Python+OpenCv如何制作證件圖片生成器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:Python+OpenCv如何制作證件圖片生成器-創(chuàng)新互聯(lián)
新聞來源:http://www.rwnh.cn/article20/cepsjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、關(guān)鍵詞優(yōu)化、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站制作、App開發(fā)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容