From bfc0db48df00c3992e3df84ae6a3ccb826bc1a2d Mon Sep 17 00:00:00 2001 From: LeLe <251192913@qq.com> Date: Tue, 24 Sep 2019 17:44:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=B4=E5=83=8F=E5=8A=A0?= =?UTF-8?q?=E5=B0=8F=E6=97=97=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=9C=A8faceimg.p?= =?UTF-8?q?y=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- faceimg.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 faceimg.py diff --git a/faceimg.py b/faceimg.py new file mode 100644 index 0000000..95d8273 --- /dev/null +++ b/faceimg.py @@ -0,0 +1,26 @@ +from PIL import Image + +def GenFaceFlag(mainPicPath,flagPath,savepath): + mainImg = Image.open(mainPicPath) #主图 + flagImg = Image.open(flagPath) #要添加的小旗 + mw,mh = mainImg.size + fw,fh = flagImg.size + if fw>(int)(mw * 0.3):#如果flag的尺寸太大则要缩放 + newwidth = (int)(mw*0.3) + newheight = (int)(mw*0.3*fh/fw) + flagImgNew=flagImg.resize((newwidth,newheight)) + else: + newwidth = fw + newheight =fh + flagImgNew = flagImg + lt_x=mw-newwidth#计算要把flag粘贴的位置 + lt_y=mh-newheight + mainImg.paste(flagImgNew,(lt_x,lt_y)) #粘贴 + mainImg.save(savepath)#保存新图像 + +if __name__=="__main__": + mainpath = "C:\\Python\\vWXCrawl\\pub\\vWeChatCrawl\\main2.jpg" + flagpath = "C:\\Python\\vWXCrawl\\pub\\vWeChatCrawl\\flag2.png" + savepath = "C:\\Python\\vWXCrawl\\pub\\vWeChatCrawl\\save.png" + + GenFaceFlag(mainpath,flagpath,savepath) \ No newline at end of file