黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

python2和python3實現在圖片上加漢字

系統 1883 0

文章目錄

  • Python2 在圖片上加漢字代碼實現
  • Python3 在圖片上加漢字代碼實現
  • 遇到的問題

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

Python2 在圖片上加漢字代碼實現

            
              # -*- 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)

    


            
          

Python3 在圖片上加漢字代碼實現

            
              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')

            
          

更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論