python2和python3實現(xiàn)在圖片上加漢字的方法
python2和python3實現(xiàn)在圖片上加漢字,最主要的區(qū)別還是內(nèi)部編碼方式不一樣導致的,在代碼上表現(xiàn)為些許的差別。理解了內(nèi)部編碼原理也就不會遇到這些問題了,以下代碼是在WIN10系統(tǒng)上時測好用的。
Python2 在圖片上加漢字代碼實現(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ā)動機轉(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)
Python3 在圖片上加漢字代碼實現(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ā)動機轉(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') 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)
這是因為這是因為默認的是utf-8編碼格式
中文字符的Unicode編碼0x0800-0xFFFF之間,(utf-8包含了部分漢字)
當你試圖將該“中文字符”轉(zhuǎn)成U碼的utf-8時超出了其范籌
而GBK 規(guī)范收錄了 ISO 10646.1 中的全部 CJK 漢字和符號,并有所補充,
所以解決方法是將utf-8改為gbk
word = unicode(word,'utf-8') 改為 word = unicode(word,'gbk')
總結
以上所述是小編給大家介紹的python2和python3實現(xiàn)在圖片上加漢字的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關文章
pytorch之torch_scatter.scatter_max()用法
這篇文章主要介紹了pytorch之torch_scatter.scatter_max()用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09python3 QT5 端口轉(zhuǎn)發(fā)工具兩種場景分析
這篇文章主要介紹了python3 QT5 端口轉(zhuǎn)發(fā)工具,功能是打開本機端口,映射到指定IP的端口,接下來通過兩種場景給大家詳細介紹,感興趣的朋友一起看看吧2022-01-01Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細教程(一看就會)
使用爬蟲爬數(shù)據(jù),總要涉及到數(shù)據(jù)持久化,也就是數(shù)據(jù)存儲的問題,下面這篇文章主要給大家介紹了關于Python爬蟲獲取數(shù)據(jù)保存到數(shù)據(jù)庫中的超詳細教程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06Pytorch損失函數(shù)nn.NLLLoss2d()用法說明
這篇文章主要介紹了Pytorch損失函數(shù)nn.NLLLoss2d()用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python基于輾轉(zhuǎn)相除法求解最大公約數(shù)的方法示例
這篇文章主要介紹了Python基于輾轉(zhuǎn)相除法求解最大公約數(shù)的方法,結合實例形式分析了Python使用輾轉(zhuǎn)相除法求解最大公約數(shù)的實現(xiàn)方法與優(yōu)化操作技巧,需要的朋友可以參考下2018-04-04python常用request庫與lxml庫操作方法整理總結
一路學習,一路總結,技術就是這樣,應用之后,在進行整理,才可以加深印象。本篇文字為小節(jié)篇,核心總結 requests 庫與 lxml 庫常用的操作2021-08-08