python2和python3實(shí)現(xiàn)在圖片上加漢字,最主要的區(qū)別還是內(nèi)部編碼方式不一樣導(dǎo)致的,在代碼上表現(xiàn)為些許的差別。理解了內(nèi)部編碼原理也就不會(huì)遇到這些問(wèn)題了,以下代碼是在WIN10系統(tǒng)上時(shí)測(cè)好用的。
建網(wǎng)站原本是網(wǎng)站策劃師、網(wǎng)絡(luò)程序員、網(wǎng)頁(yè)設(shè)計(jì)師等,應(yīng)用各種網(wǎng)絡(luò)程序開(kāi)發(fā)技術(shù)和網(wǎng)頁(yè)設(shè)計(jì)技術(shù)配合操作的協(xié)同工作。創(chuàng)新互聯(lián)專業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站制作(企業(yè)站、自適應(yīng)網(wǎng)站建設(shè)、電商門(mén)戶網(wǎng)站)等服務(wù),從網(wǎng)站深度策劃、搜索引擎友好度優(yōu)化到用戶體驗(yàn)的提升,我們力求做到極致!Python2 在圖片上加漢字代碼實(shí)現(xiàn)# -*- coding: cp936 -*- import cv2 import numpy as np from PIL import Image, ImageDraw, ImageFont def ID_2_Word(txt): tmp_ID = txt.split(':')[0] value = txt.split(':')[-1] ''' numbers = { 'DS041' : "Coolant TEMP ", 'DS048' : "RPM ", 'DS049' : "Speed ", 'DS098' : "Oil level ", 'DS123' : "Control Module Voltage" } ''' numbers = { 'DS041' : "冷卻液溫度", 'DS048' : "發(fā)動(dòng)機(jī)轉(zhuǎn)速", 'DS049' : "車速 ", 'DS098' : "燃油液位輸入", 'DS123' : "控制模塊電壓" } word = numbers.get(tmp_ID, None) result = str(word) + ':' + value #print(result) return result def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20): if (isinstance(img, np.ndarray)): #判斷是否OpenCV圖片類型 img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img) #fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="utf-8") fontText = ImageFont.truetype("font/simsun.ttc", textSize, encoding="gb2312") #cp936 draw.text((left, top), text, textColor, font=fontText) return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) def layer1_show(img,data): frame = cv2.resize(img, (1280, 720), interpolation=cv2.INTER_CUBIC) font = ImageFont.truetype('font/simsun.ttc',24,encoding="utf-8") OBD_string = data y0, dy = 50, 25 for i, txt in enumerate(OBD_string.split(';')): #word = txt word = ID_2_Word(txt) #將OBD信號(hào)的ID轉(zhuǎn)換為中文 word = unicode(word,'gbk') #print(i, txt.split(':')[0]) y = y0+i*dy frame = cv2ImgAddText(frame, word, 100, y, (255, 0, 0), 20) cv2.imshow("layer_1", frame) cv2.waitKey(0) if __name__ == '__main__': img = cv2.imread("map.png"); data = "DS041: 88;DS048: 800;DS049: 64;DS098: 0.00;DS123: 0.00" layer1_show(img,data)