您好,登錄后才能下訂單哦!
python2和python3實現在圖片上加漢字,最主要的區別還是內部編碼方式不一樣導致的,在代碼上表現為些許的差別。理解了內部編碼原理也就不會遇到這些問題了,以下代碼是在WIN10系統上時測好用的。
# -*- 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' : "發動機轉速", '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轉換為中文 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)
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' : "發動機轉速", '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轉換為中文 #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: ‘ascii' codec can't decode byte 0xe8 in position 0: ordinal not in range(128)
這是因為這是因為默認的是utf-8編碼格式
中文字符的Unicode編碼0x0800-0xFFFF之間,(utf-8包含了部分漢字)
當你試圖將該“中文字符”轉成U碼的utf-8時超出了其范籌
而GBK 規范收錄了 ISO 10646.1 中的全部 CJK 漢字和符號,并有所補充,
所以解決方法是將utf-8改為gbk
word = unicode(word,'utf-8') 改為 word = unicode(word,'gbk')
總結
以上所述是小編給大家介紹的python2和python3實現在圖片上加漢字的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。