import os
from PIL import Image, ImageFilter
class MyGaussianBlur(ImageFilter.Filter):
name = "GaussianBlur"
def __init__(self, radius=2, bounds=None):
self.radius = radius
self.bounds = bounds
def filter(self, image):
if self.bounds:
clips = image.crop(self.bounds).gaussian_blur(self.radius)
image.paste(clips, self.bounds)
return image
else:
return image.gaussian_blur(self.radius)
#源目录
input_Path = '原始图像路径'
#输出目录
Output_Path = '图像修改后保存路径'
def processImage(filesoure, destsoure, name, imgtype):
imgtype = 'jpeg' if imgtype == '.jpg' else 'png'
#打开图片
im = Image.open(filesoure + name)
#高斯模糊
image = im.filter(MyGaussianBlur(radius=2.0))
image.save(destsoure + name, imgtype)
def run():
#切换到源目录,遍历源目录下所有图片
os.chdir(input_Path)
for i in os.listdir(os.getcwd()):
#检查后缀
postfix = os.path.splitext(i)[1]
if postfix == '.jpg' or postfix == '.png':
processImage(input_Path, Output_Path, i, postfix)
if __name__ == '__main__':
run()
利用python对批量图像进行自定义模糊半径的高斯模糊
最新推荐文章于 2024-08-21 19:45:00 发布