用 Python 把你的朋友变成表情包
俊红的数据分析之路
共 8268字,需浏览 17分钟
·
2021-05-10 09:03
来源:数据分析1080
在日常生活中,我们经常会存取一些朋友们的丑照,在这个项目中,我们以萌萌哒的熊猫头作为背景,然后试着在背景图上加入朋友们的照片。效果如下图所示:
导入朋友的照片(前景照片); 处理前景照片(缩放、旋转,填充); 导入熊猫头照片(背景照片); 将前景和背景拼接起来形成表情包; 在表情包下面添加文字。
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)
![](https://filescdn.proginn.com/9f733e185d3b2d8bed4bbe34021c7cad/d5b382ea3fe2bd71d51056caa413411f.webp)
4、等比例缩放前景照片
因为我们发现前景照片的尺寸比背景尺寸还要大,这显然是不合适的,所以要先对其进行等比例(0.3)缩放。
image_resize = cv2.resize(image, None, fx=0.3, fy=0.3, interpolation = cv2.INTER_CUBIC)
plt_show(image_resize)
![](https://filescdn.proginn.com/ad0184299a7ff343d7a0e79ad8a28d54/66fb82d0bc000e45bc3771c2ffa32964.webp)
5、对前景照片进行二值化处理
ret, image_binary = cv2.threshold(image_resize, 80, 255, cv2.THRESH_BINARY)
plt_show(image_binary)
![](https://filescdn.proginn.com/fc975b4a4481a603c53fc92156ccf295/0fba6e22b9dd482d3a34b5d5992d2b7d.webp)
6、提取出感兴趣区域
image_roi = image_binary[74: 185, 0: 150]
plt_show(image_roi)
![](https://filescdn.proginn.com/a2a191760176ec6b89fda2da0f0ec13c/b134e2fa2b52f2ad80d7d11c9f013f35.webp)
7、旋转图片
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)
![](https://filescdn.proginn.com/efd11c12a0f12eb1acd5098b1e438442/9bd149a2aab3da53f7217e97e254ce21.webp)
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)
![](https://filescdn.proginn.com/80855cdd736fc737e5f9560b42fb23da/87f89f2f015d96ac2d2f6c99848bf551.webp)
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)
![](https://filescdn.proginn.com/f2b99aacaedba767efd28d514e8a6906/4c3128d231e7e73dc61b684331e9dc41.webp)
![](https://filescdn.proginn.com/293dca861512b25247f567678d1701dc/ae8854bfcba5712da1c87be7a353c386.webp)
10、导入背景图片
background = cv2.imread('back.jpg', 0)
plt_show(background)
![](https://filescdn.proginn.com/6b78cdf0fa258cb36c2d37fa39f8eadd/0b6f27f73b1e91e49cdf65fa078c7b2e.webp)
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)
![](https://filescdn.proginn.com/2ddb4716f00046ca3acc3155dba0cdcd/8299525b54c2274c8297324fe88a80af.webp)
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)
![](https://filescdn.proginn.com/dfb02dc0f184848c7a151f7c55eb1dd6/860aba000ba52dc37ba61d91aa716ae8.webp)
12.2 添加中文文本
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)
![](https://filescdn.proginn.com/7f865a0d950a0320e3242fc5c8112668/5b8b8d47ed7595e098f583cf41eaa59a.webp)
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)) # 保存表情包
![音符](https://filescdn.proginn.com/a3a56f4b99d8cf52e159db12b45b57f1/ab794174844a01cffa1261b97f5823a0.webp)
![](https://filescdn.proginn.com/21c1a0c486e04b048c87316b0a2e48e6/7c41935426c0491c0664aea21efbdf5e.webp)
![](https://filescdn.proginn.com/1cc4c1e44d8af7bbca13461904ee62d3/34873b695b3a7eb97d4c04ab81ed0f3d.webp)
![](https://filescdn.proginn.com/206d874800deba39a6e26bacd19948d4/87e8a3fec9ad7bdc2d4c77a98c13a112.webp)
![](https://filescdn.proginn.com/d32d16fab5e8a7a79e6b91714b7afbfb/8896c7507f0859068e82ce39e035b4f3.webp)
评论