文章目錄
- Python2 在圖片上加漢字代碼實(shí)現(xiàn)
- Python3 在圖片上加漢字代碼實(shí)現(xiàn)
- 遇到的問題
python2和python3實(shí)現(xiàn)在圖片上加漢字,最主要的區(qū)別還是內(nèi)部編碼方式不一樣導(dǎo)致的,在代碼上表現(xiàn)為些許的差別。理解了內(nèi)部編碼原理也就不會(huì)遇到這些問題了,以下代碼是在WIN10系統(tǒng)上時(shí)測好用的。
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)
Python3 在圖片上加漢字代碼實(shí)現(xiàn)
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')
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)
遇到的問題
python2中:UnicodeDecodeError: ‘a(chǎn)scii’ codec can’t decode byte 0xe8 in position 0: ordinal not in range(128)
這是因?yàn)檫@是因?yàn)槟J(rèn)的是utf-8編碼格式
中文字符的Unicode編碼0x0800-0xFFFF之間,(utf-8包含了部分漢字)
當(dāng)你試圖將該“中文字符”轉(zhuǎn)成U碼的utf-8時(shí)超出了其范籌
而GBK 規(guī)范收錄了 ISO 10646.1 中的全部 CJK 漢字和符號(hào),并有所補(bǔ)充,
所以
解決方法是將utf-8改為gbk
word = unicode(word,'utf-8') 改為 word = unicode(word,'gbk')
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
