实现步骤导入好友照片(前景照片);处理前景照片(缩放、旋转、填充);导入熊猫头像照片(背景照片);将前景和背景拼接在一起形成模因;在模因下方添加文字。Python实现1.导入需要的库importcv2importnumpyasmpimportmatplotlib.pyplotaspltfromPILimportImage,ImageDraw,ImageFont这个项目主要是通过opencv完成的,但是如果要在表情包下写中文,PIL(pillow)图书馆是必要的。2.绘图函数这里写一个绘图函数,方便绘图操作。defplt_show(img):imageRGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)plt.imshow(imageRGB)plt.show()3.导入前景照片image=cv2.imread('fore.jpg',0)#import灰度图可以是plt_show(image)4.按比例缩放前景照片因为我们发现前景照片的尺寸大于背景尺寸,这显然不合适,所以必须先按比例(0.3)缩放。image_resize=cv2.resize(image,None,fx=0.3,fy=0.3,interpolation=cv2.INTER_CUBIC)plt_show(image_resize)5.前景照片二值化这里我们设置像素值大于70的区域为255;小于70的区域设置为0。ret,image_binary=cv2.threshold(image_resize,70,255,cv2.THRESH_BINARY)plt_show(image_binary)6.提取感兴趣区域image_roi=image_binary[250:550,90:380]plt_show(image_roi)7.旋转图片因为我们的背景图片(熊猫头)是正的,而前景图片是向右倾斜的,所以我们需要先旋转一下(逆时针旋转15度左右)。rows,cols=image_roi.shapeM=cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),15,1)#(旋转中心,逆时针旋转角度,所有方向均等放大)image_rotate=cv2.warpAffine(image_roi,M,(280,275))#(280,275)指的是旋转画布的大小plt_show(image_rotate)8.删除一些不需要的黑色区域这里我们使用cv2。fillPoly函数用白色填充不需要的区域。image_rotate_copy=image_rotate.copy()pts1=np.array([[0,33],[122,0],[0,0]],np.int32)pts2=np.array([[0,145],[0,h],[30,h]],np.int32)pts3=np.array([[180,h],[w,h],[w,75]],np.int32)pts4=np.array([[250,0],[w,0],[w,100]],np.int32)foreground=cv2.fillPoly(image_rotate_copy,[pts1,pts2,pts3,pts4],(255,255,255))#(图片,填充区域,填充颜色)plt_show(foreground)9.导入背景图片background=cv2.imread('back.jpg',0)plt_show(background)10.将两张图片组合成表情包h_f,w_f=foreground.shapeh_b,w_b=background.shapeleft=(w_b-w_f)//2#前景图片左侧横坐标在背景图片中right=left+w_f#前景右侧横坐标imageinthebackgroundimagetop=80#前景图片在背景图片上方的纵坐标bottom=top+h_f#前景图片在背景图片下方的纵坐标emoji=backgroundemoji[top:bottom,left:right]=foregroundplt_show(emoji)11、在表情包下添加文字11.1添加英文文字如果只想添加英文文字,可以使用opencv解决:http://www.kaifx.cn/activity/emoji_copy=emoji.copy()#(图片,文字,位置,字体,文字大小,文字颜色,文字粗细)cv2.putText(emoji_copy,"hhh",(230,500),cv2.FONT_HERSHEY_SIMPLEX,1.2,(0,0,0),5)plt_show(emoji_copy)11.2添加中文文本如果要添加中文文本,我们需要使用PIL库来实现PilImg=Image.fromarray(emoji)#cv2toPILdraw=ImageDraw.Draw(PilImg)#创建画笔ttfront=ImageFont.truetype('simhei.ttf',34)#设置字体draw.text((210,450),"Heyhey",fill=0,font=ttfront)#(position,text,textcolor,font)emoji_text=cv2.cvtColor(np.array(PilImg),cv2.COLOR_RGB2BGR)#PIL切换回cv2plt_show(emoji_text)12、保存表情包cv2.imwrite('./emoji.png',np.array(emoji_text))1完整代码importcv2importnumpyasmpimportmatplotlib.pyplotaspltfromPILimportImage,ImageDraw,ImageFontdefplt_show(img):imageRGB=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)plt.imshow(imageRGB)plt.show()image=cv2.imread('fore.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,70,255,cv2.THRESH_BINARY)#图像二值化image_roi=image_binary[250:550,90:380]#感兴趣区域行,cols=image_roi.shape#旋转M=cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),15,1)image_rotate=cv2.warpAffine(image_roi,M,(280,275))#填充不需要的区域h,w=image_rotate.shapeimage_rotate_copy=image_rotate.copy()pts1=np.array([[0,33],[122,0],[0,0]],np.int32)pts2=np.array([[0,145],[0,h],[30,h]],np.int32)pts3=np.array([[180,h],[w,h],[w,75]],np.int32)pts4=np.array([[250,0],[w,0],[w,100]],np.int32)foreground=cv2.fillPoly(image_rotate_copy,[pts1,pts2,pts3,pts4],(255,255,255))background=cv2.imread('back.jpg',0)#导入背景图片#拼接两张图片h_f,w_f=foreground.shapeh_b,w_b=background.shapeleft=(w_b-w_f)//2right=left+w_ftop=80bottom=top+h_femoji=backgroundemoji[top:bottom,left:right]=foregroundPilImg=Image.fromarray(emoji)#cv2toPILdraw=ImageDraw.Draw(PilImg)#创建画笔ttfront=ImageFont.truetype('simhei.ttf',34)#设置字体draw.text((210,450),"嘿嘿嘿",fill=0,font=ttfront)#(position,text,textcolor,font)emoji_text=cv2.cvtColor(np.array(PilImg),cv2.COLOR_RGB2BGR)#PIL转换回来tocv2cv2.imwrite('./emoji.png',np.array(emoji_text))#保存表情包
