您現在的位置是:首頁 > 舞蹈首頁舞蹈

用 Python 把你的朋友變成表情包

由 Python部落 發表于 舞蹈2021-07-08
簡介fillPoly(image_rotate_copy, [pts6], (255, 255, 255))plt_show(foreground)9、再次提取感興趣區域並縮放foreground_roi = foreground[0: 93,

怎麼旋轉畫布

https://blog。csdn。net/qq_36758914/article/details/106303928

一、專案說明

在日常生活中,我們經常會存取一些朋友們的醜照,在這個專案中,我們以萌萌噠的熊貓頭作為背景,然後試著在背景圖上加入朋友們的照片。效果如下圖所示:

用 Python 把你的朋友變成表情包

用 Python 把你的朋友變成表情包

用 Python 把你的朋友變成表情包

二、實現步驟

匯入朋友的照片(前景照片);

處理前景照片(縮放、旋轉,填充);

匯入熊貓頭照片(背景照片);

將前景和背景拼接起來形成表情包;

在表情包下面新增文字。

三、Python 實現

1、匯入需要的庫

import cv2

import numpy as mp

import matplotlib。pyplot as plt

from PIL import Image, ImageDraw, ImageFont

這個專案主要是透過 opencv 完成,但如果要在表情包下面寫中文的話,PIL(pillow)庫是必不可少的。

2、繪圖函式

這裡寫一個繪圖函式,方便繪圖操作。

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)

用 Python 把你的朋友變成表情包

4、等比例縮放前景照片

因為我們發現前景照片的尺寸比背景尺寸還要大,這顯然是不合適的,所以要先對其進行等比例(0。3)縮放。

image_resize = cv2。resize(image, None, fx=0。3, fy=0。3, interpolation = cv2。INTER_CUBIC)

plt_show(image_resize)

用 Python 把你的朋友變成表情包

5、對前景照片進行二值化處理

在這裡,我們將畫素值大於 80 的區域設定為 255;小於 80 的區域設定成 0。

ret, image_binary = cv2。threshold(image_resize, 80, 255, cv2。THRESH_BINARY)

plt_show(image_binary)

用 Python 把你的朋友變成表情包

6、提取出感興趣區域

image_roi = image_binary[74: 185, 0: 150]

plt_show(image_roi)

用 Python 把你的朋友變成表情包

7、旋轉圖片

因為我們的背景圖片(熊貓頭)是正的,而前景圖片有些向右傾斜,所以要先對其進行旋轉操作(大概逆時針旋轉 15 度即可)。

rows, cols = image_roi。shape

M = 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)

用 Python 把你的朋友變成表情包

8、將一些不需要的黑色區域刪除掉

在這裡我們使用 cv2。fillPoly 函式對不需要的區域用白色進行填充。

h, w = image_rotate。shape

image_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)) # (圖片,填充區域,填充顏色)

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)

用 Python 把你的朋友變成表情包

9、再次提取感興趣區域並縮放

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)

用 Python 把你的朋友變成表情包

用 Python 把你的朋友變成表情包

10、匯入背景圖片

background = cv2。imread(‘back。jpg’, 0)

plt_show(background)

用 Python 把你的朋友變成表情包

11、組合兩張圖片成表情包

h_f, w_f = foreground。shape

h_b, w_b = background。shape

left = (w_b - w_f)//2 # 前景圖片在背景圖片中的左邊的橫座標

right = left + w_f # 前景圖片在背景圖片中的右邊的橫座標

top = 100 # 前景圖片在背景圖片中的上邊的縱座標

bottom = top + h_f # 前景圖片在背景圖片中的下邊的縱座標

emoji = background

emoji[top: bottom, left: right] = foreground

plt_show(emoji)

用 Python 把你的朋友變成表情包

12、在表情包下面新增文字

12.1 新增英文文字

如果只是要新增英文文字,用 opencv 就可以解決:

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)

用 Python 把你的朋友變成表情包

12.2 新增中文文字

如果要新增中文文字,我們需要藉助 PIL 庫來實現。

PilImg = Image。fromarray(emoji) # cv2 轉 PIL

draw = ImageDraw。Draw(PilImg) # 建立畫筆

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 轉回 cv2

plt_show(emoji_text)

用 Python 把你的朋友變成表情包

13、儲存表情包

cv2。imwrite(‘。/emoji。png’, np。array(emoji_text))

四、完整程式碼

import cv2

import numpy as mp

import matplotlib。pyplot as plt

from PIL import Image, ImageDraw, ImageFont

def plt_show(img):

imageRGB = cv2。cvtColor(img, cv2。COLOR_BGR2RGB)

plt。imshow(imageRGB)

plt。show

image = cv2。imread(‘SXC。jpg’, 0) # 匯入前景圖片

image_resize = cv2。resize(image, None, fx=0。3, fy=0。3, interpolation = cv2。INTER_CUBIC) # 縮放

ret, image_binary = cv2。threshold(image_resize, 80, 255, cv2。THRESH_BINARY) # 圖片二值化

image_roi = image_binary[74: 185, 0: 150] # 感興趣區域

rows, cols = image_roi。shape

# 旋轉

M = cv2。getRotationMatrix2D(((cols-1)/2。0, (rows-1)/2。0), 15, 1)

image_rotate = cv2。warpAffine(image_roi, M, (140, 130))

# 填充不需要的區域

h, w = image_rotate。shape

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

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

foreground_roi = foreground[0: 93, 0: 125]

foreground_roi_resize = cv2。resize(foreground_roi, None, fx=2。5, fy=2。5, interpolation = cv2。INTER_CUBIC)

background = cv2。imread(‘back。jpg’, 0) # 匯入背景圖片

# 拼接兩張圖片

h_f, w_f = foreground_roi_resize。shape

h_b, w_b = background。shape

left = (w_b - w_f)//2

right = left + w_f

top = 80

bottom = top + h_f

emoji = background

emoji[top: bottom, left: right] = foreground_roi_resize

PilImg = Image。fromarray(emoji) # cv2 轉 PIL

draw = ImageDraw。Draw(PilImg) # 建立畫筆

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 轉回 cv2

cv2。imwrite(‘。/emoji。png’, np。array(emoji_text)) # 儲存表情包

微軟於年初推出了自己的Python教程,我們將其漢化提供給大家,歡迎大家收藏關注哦~(已經漢化完成的20集,我們日更1集,未完成部分我們儘快更新)