一、项目说明
在日常生活中,我们经常会存取一些朋友们的丑照,在这个项目中,我们以萌萌哒的熊猫头作为背景,然后试着在背景图上加入朋友们的照片。效果如下图所示:
二、实现步骤
-
导入朋友的照片(前景照片);
-
处理前景照片(缩放、旋转,填充);
-
导入熊猫头照片(背景照片);
-
将前景和背景拼接起来形成表情包;
-
在表情包下面添加文字。
三、Python 实现
1、导入需要的库
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 中使用 scipy.interpolate 模块?09/28
- ♥ 实例讲解join方法的使用12/02
- ♥ 如何在python中使用卡方分布?09/23
- ♥ 学完python后有哪些工作10/28
- ♥ python3中如何实现一行输入,用空格隔开10/19
- ♥ 如何在python中使用popitem09/21
内容反馈