python 圖片處理 根據4個座標圍成的區域填充顏色,並填入文字,簡單案例

運行環境:python3.6

第三方庫:PIL (安裝命令:pip install pillow)

該項目主要目的:將圖中中文替換成英文

案例圖片:

                                                            444.png

圖片中部分文字的座標 字典數據:(圖片左上角是原點(0,0),x軸是橫軸,y軸是縱軸。)

dic={"尺碼":[92,150,129,150,129,173,92,173],
     "衣長":[212,150,253,150,253,172,212,172],
     "肩寬":[329,150,370,150,370,173,329,173],
     "胸圍":[450,150,485,150,485,174,450,174],
     "袖長":[574,149,610,149,610,173,574,173]}

大致思路:

第一步:將該文字區域填充成背景色。

第二部:在該區域填入對應的英文

 

填充背景色效果圖:

填入英文效果圖如下:

                                                            new444.png

 

完整代碼如下:

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import os

# 找到文字區域,修改背景顏色,然後填寫英文
def change_photo(all_lists,path_dir,filename):
    # 拼接圖片目錄
    path=os.path.join(path_dir,filename)
    # 打開圖片
    img = Image.open(path)
    # 圖片寬度
    width = img.size[0]
    # 圖片高度
    height = img.size[1]
    RGB_1 = []
    RGB_2 = []
    RGB_3 = []
    for j in all_lists:
        lists = all_lists[j]
        # 獲取第1個座標的顏色RGBA的值
        data1 = (img.getpixel((lists[0], lists[1])))
        # 獲取第2個座標的顏色RGBA的值
        data2 = (img.getpixel((lists[2], lists[3])))
        # 獲取第3個座標的顏色RGBA的值
        data3 = (img.getpixel((lists[4], lists[5])))
        # 獲取第4個座標的顏色RGBA的值
        data4 = (img.getpixel((lists[5], lists[7])))
        #print(data1,data2,data3,data4)
        RGB_1+=[data1[0],data2[0],data3[0],data4[0]]
        RGB_2 += [data1[1], data2[1], data3[1], data4[1]]
        RGB_3 += [data1[2], data2[2], data3[2], data4[2]]
    # 按從小到大排序
    RGB_1.sort()
    RGB_2.sort()
    RGB_3.sort()
    # 取出顏色中間值
    RGB_1=RGB_1[int(len(RGB_1)/2)]
    RGB_2 = RGB_2[int(len(RGB_2) / 2)]
    RGB_3 = RGB_3[int(len(RGB_3) / 2)]
    # 組成最可能的背景色
    RGB_data=(RGB_1,RGB_2,RGB_3)
    # 根據背景色選擇文字顏色
    if (RGB_1 * 0.299 + RGB_2 * 0.578 + RGB_3 * 0.114) >= 192: #淺色
        words_colour=(0,0,0) # 設置文字顏色爲黑色
    else:
        words_colour=(255,255,255) # 設置文字顏色爲白色

    # 對每個座標區域顏色修改-背景色
    for i in all_lists:
        lists=all_lists[i]
        add_right_size = int((lists[7] - lists[1]) * 1/ 5)
        lists[2] += add_right_size
        lists[4] += add_right_size
        for x in range(0,width):
            for y in range(0,height):
                if (x>lists[0] and y>lists[1]) and (x<lists[2] and y>lists[3]) and (x<lists[4] and y<lists[5]) and (x>lists[6] and y<lists[7]) :
                    # 將該區域顏色改成背景色
                    img.putpixel((x,y),RGB_data)
    img = img.convert("RGB")#把圖片強制轉成RGB
    # 填充英文
    for i in all_lists:
        lists = all_lists[i]
        # 寫字位置下調五分之一高度
        add_low_size = int((lists[7] - lists[1]) * 1 / 5)
        # 設置所使用的字體,高度
        font_size = int((lists[7] - lists[1]) * 2 / 3)
        font = ImageFont.truetype("C:\Windows\Fonts\Arial.ttf", font_size)
        # 畫圖
        draw = ImageDraw.Draw(img)
        draw.text((lists[0], lists[1]+add_low_size), i, words_colour, font=font)  # 設置文字位置/內容/顏色/字體
        draw = ImageDraw.Draw(img)  #
    # 圖片處理後,生成新的名稱,加前綴"new"
    new_path=os.path.join(path_dir,"new"+filename)
    # 另存圖片
    img.save(new_path)#保存圖片
    return True


if __name__ == '__main__':
    # 案例圖片所在目錄
    path_dir = r'C:/Users/Administrator/Desktop/AIphoto/photo_test'
    # 444
    dic={"尺碼":[92,150,129,150,129,173,92,173],
         "衣長":[212,150,253,150,253,172,212,172],
         "肩寬":[329,150,370,150,370,173,329,173],
         "胸圍":[450,150,485,150,485,174,450,174],
         "袖長":[574,149,610,149,610,173,574,173]}
    change_photo(dic,path_dir,'444.png')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章