脚本专栏 发布日期:2025/2/14 浏览次数:1
在介绍完给图上添加文字后,我们再介绍给图片上添加图片,也就是图片的叠加。
需要使用的Python的图像库:PIL.更加详细的知识点如下:
Imaga模块:用来创建,打开,保存图片文件
这些函数中,只有最后一个函数是新介绍的,其它的函数,我们以前介绍过。
下面是完整的代码,请参考:
from PIL import Image def addImg(img): markImg = Image.new('RGBA',(120,120),'white') img.paste(markImg,(0,0)) img.save(path) path = input("Please input the image file with path: ") try: print("path: "+path) oriImg = Image.open(path) addImg(oriImg) oriImg.show() except IOError: print("can't open the file,check the path again") newImg = Image.new('RGBA',(320,240),'blue') newImg.save(path)
在代码中,我们先创建了一个320*240的蓝色图片,然后再创建一个120*120的白色图片。
通过paste函数把白色图片添加到了蓝色图片中,添加的位置位于蓝色图片左上角。
下面是程序的运行结果,请参考:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。