python2和python3實現(xiàn)在圖片上加漢字,最主要的區(qū)別還是內(nèi)部編碼方式不一樣導(dǎo)致的,在代碼上表現(xiàn)為些許的差別。理解了內(nèi)部編碼原理也就不會遇到這些問題了,以下代碼是在WIN10系統(tǒng)上時測好用的。

# -*- 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ā)動機轉(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信號的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)
網(wǎng)站名稱:python2和python3實現(xiàn)在圖片上加漢字的方法-創(chuàng)新互聯(lián)
標題網(wǎng)址:http://www.chinadenli.net/article34/iiipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、移動網(wǎng)站建設(shè)、外貿(mào)建站、響應(yīng)式網(wǎng)站、小程序開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容