利用python对批量图像进行自定义模糊半径的高斯模糊

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()
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值