把你的朋友變成表情包?Python:So easy
點擊上方“Python 知識大全”,選擇“加為星標”
第一時間關注Python技術干貨!
號外:
本號免費提供 CSDN 資源下載,需要的伙伴公眾號后臺回復【CSDN】




實現(xiàn)步驟
導入朋友的照片(前景照片);
處理前景照片(縮放、旋轉,填充);
導入熊貓頭照片(背景照片);
將前景和背景拼接起來形成表情包;
在表情包下面添加文字。
1、導入需要的庫
import cv2import numpy as mpimport matplotlib.pyplot as pltfrom?PIL?import?Image,?ImageDraw,?ImageFont
2、繪圖函數(shù)
def plt_show(img):imageRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)plt.imshow(imageRGB)????plt.show()
3、導入前景照片
image = cv2.imread('SXC.jpg', 0) # 導入灰度圖即可plt_show(image)

4、等比例縮放前景照片
image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation = cv2.INTER_CUBIC)plt_show(image_resize)

5、對前景照片進行二值化處理
ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)plt_show(image_binary)

6、提取出感興趣區(qū)域
image_roi = image_binary[74: 185, 0: 150]plt_show(image_roi)

7、旋轉圖片
rows, cols = image_roi.shapeM = cv2.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 15, 1) # (旋轉中心,逆時針旋轉角度,各個方向同等擴大比例)image_rotate = cv2.warpAffine(image_roi, M, (140, 130)) # (140, 130) 是指旋轉后的畫布大小plt_show(image_rotate)

8、將一些不需要的黑色區(qū)域刪除掉
h, w = image_rotate.shapeimage_rotate_copy = image_rotate.copy()pts1 = np.array([[0, 20], [64, 0], [0, 0]], np.int32)pts2 = np.array([[0, 18], [0, h], [80, h]], np.int32)pts3 = np.array([[0, 100], [0, h], [w, h], [w, 100]], np.int32)pts4 = np.array([[111, 0], [w, 0], [w, 30]], np.int32)pts5 = np.array([[124, 0], [115, h], [w, h]], np.int32)pts6 = np.array([[120, 40], [95, 100], [120, 100]], np.int32)foreground = cv2.fillPoly(image_rotate_copy, [pts1], (255, 255, 255)) # (圖片,填充區(qū)域,填充顏色)foreground = cv2.fillPoly(image_rotate_copy, [pts2], (255, 255, 255))foreground = cv2.fillPoly(image_rotate_copy, [pts3], (255, 255, 255))foreground = cv2.fillPoly(image_rotate_copy, [pts4], (255, 255, 255))foreground = cv2.fillPoly(image_rotate_copy, [pts5], (255, 255, 255))foreground = cv2.fillPoly(image_rotate_copy, [pts6], (255, 255, 255))plt_show(foreground)

9、再次提取感興趣區(qū)域并縮放
foreground_roi = foreground[0: 93, 0: 125]plt_show(foreground_roi)foreground_roi_resize = cv2.resize(foreground_roi, None, fx=2.5, fy=2.5, interpolation = cv2.INTER_CUBIC)plt_show(foreground_roi_resize)


10、導入背景圖片
background = cv2.imread('back.jpg', 0)plt_show(background)

11、組合兩張圖片成表情包
h_f, w_f = foreground.shapeh_b, w_b = background.shapeleft = (w_b - w_f)//2 # 前景圖片在背景圖片中的左邊的橫坐標right = left + w_f # 前景圖片在背景圖片中的右邊的橫坐標top = 100 # 前景圖片在背景圖片中的上邊的縱坐標bottom = top + h_f # 前景圖片在背景圖片中的下邊的縱坐標emoji = backgroundemoji[top: bottom, left: right] = foregroundplt_show(emoji)

12、在表情包下面添加文本
12.1 添加英文文本
emoji_copy = emoji.copy()# (圖片,文本,位置,字體,文本大小,文本顏色,文本粗細)cv2.putText(emoji_copy, "FXXK!!", (210, 500), cv2.FONT_HERSHEY_SIMPLEX, 1.2, (0, 0, 0), 5)plt_show(emoji_copy)

12.2 添加中文文本
PilImg = Image.fromarray(emoji) # cv2 轉 PILdraw = ImageDraw.Draw(PilImg) # 創(chuàng)建畫筆ttfront = ImageFont.truetype('simhei.ttf', 34) # 設置字體draw.text((210, 450),"你瞅啥?。?,fill=0, font=ttfront) # (位置,文本,文本顏色,字體)emoji_text = cv2.cvtColor(np.array(PilImg),cv2.COLOR_RGB2BGR) # PIL 轉回 cv2plt_show(emoji_text)

13、保存表情包
cv2.imwrite('./emoji.png',?np.array(emoji_text))
推薦閱讀 一個非常好用的 Python 魔法庫【文末查看上周中獎者】 神器在手,給變量命名從此高大上!
關注「Python 知識大全」,做全棧開發(fā)工程師 歲月有你 惜惜相處
回復 【資料】獲取高質(zhì)量學習資料
評論
圖片
表情
