高斯模糊-批处理

  高斯模糊网上代码一大堆,方法各异,这里就不讲高斯模糊原理了。试着对一个文件夹所有图片进行高斯模糊。

from PIL import Image, ImageFilter
import os
import glob
 
class GaussianBlur(ImageFilter.Filter):

#设置高斯模糊半径,半径越大,越模糊
    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)
 
 
def ReadImgFile(path, suffix):
        wsi_mask_paths = glob.glob(os.path.join(path, suffix))
        wsi_mask_paths.sort()
        return wsi_mask_paths
 
 #读取文件夹下所有图片文件
imgfiles = ReadImgFile('/home/wu/Desktop/c and d/train/cat', '*.jpg')
 
for imgfile in imgfiles:
        source_img = imgfile

#对模糊后的图片重命名,网上有的资料在这里处理的很随意,最后只看到一张模糊图片,图片名称重复,导致前面处理的图片被覆盖了
        des_img =  imgfile.split(".")[0].split("\\")[-1] +"."+ imgfile.split(".")[1].split("\\")[-1]+"_3blur.jpg"
        print(des_img)
        image = Image.open(source_img)
        image = image.filter(GaussianBlur(radius=3))
        image.save(des_img)
print("Done!")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值