想法散文简友广场

python基于类继承实现滤波器使用效果并使用pillow实现图

2021-11-14  本文已影响0人  Cache_wood
import glob
import os
import matplotlib.pyplot as plt
from PIL import Image, ImageFilter

#基类Filter
class Filter:
    def __init__(self,image,parameters):
        self.image = image
        self.parameters = parameters
    def filter(self):
        pass

#边缘提取子类Edge
class Edge(Filter):
    def __init__(self,image,parameters):
        super(Edge,self).__init__(image,parameters)
        
    def filter(self,img):
        img = img.filter(ImageFilter.FIND_EDGES)
        return img

#锐化子类Sharpen
class Sharpen(Filter):
    def __init__(self,image,parameters):
        super(Sharpen,self).__init__(image,parameters)
        
    def filter(self,img):
        img = img.filter(ImageFilter.SHARPEN)
        return img

#模糊子类Blur
class Blur(Filter):
    def __init__(self,image,parameters):
        super(Blur,self).__init__(image,parameters)
        
    def filter(self,img):
        img = img.filter(ImageFilter.BLUR)
        return img

#大小调整子类Resize
class Resize(Filter):
    def __init__(self,image,parameters):
        super(Resize,self).__init__(image,parameters)
        
    def filter(self,img):
        img = img.resize((self.parameters[0],self.parameters[1]))
        return img

#具体批量处理图片类ImageShop
class ImageShop:
    def __init__(self,layout,file,lis,process):
        self.layout = layout
        self.file = file
        self.lis = lis
        self.process = process
 
    def load_images(self): #加载所有图片的路径传给lis
        self.lis = glob.glob(os.path.join(self.file,'*'+self.layout))

    def __batch_ps(self,Filter):  #批量处理图片的内部方法
        for each in range(len(self.process)):
            img = Filter.filter(self.process[each])
            self.process[each] = img
        #print(self.process)

    def batch_ps(self,operation,*args): #处理图片的外部方法,调用__batch_ps
        ImageShop.load_images(self)   #加载图片路径
        for num in self.lis:
            self.process.append(Image.open(num))
        if operation == 'Edge':   #检测具体的处理方法并调用__batch_ps
            e = Edge(image,parameters)
            ImageShop.__batch_ps(self,e)
        elif operation == 'Sharpen':
            s = Sharpen(image,parameters)
            ImageShop.__batch_ps(self,s)
        elif operation == 'Blur':
            b = Blur(image,parameters)
            ImageShop.__batch_ps(self,b)
        elif operation == 'Resize':
            r = Resize(image,parameters)
            ImageShop.__batch_ps(self,r)
        
        if len(args)>0:  #多参数处理同一张图
            for arg in args:
                if arg == 'Edge': 
                    e = Edge(image,parameters)
                    ImageShop.__batch_ps(self,e)
                elif arg == 'Sharpen':
                    s = Sharpen(image,parameters)
                    ImageShop.__batch_ps(self,s)
                elif arg == 'Blur':
                    b = Blur(image,parameters)
                    ImageShop.__batch_ps(self,b)
                elif arg == 'Resize':
                    r = Resize(image,parameters)
                    ImageShop.__batch_ps(self,r)
        #print(self.process)
        
    def display(self,row=3,column=4,maximum=60): #利用subplot函数批量显示处理后图片
        if len(self.process)>maximum:
            self.process = self.process[:maximum]  #控制最大显示图片数
        for num in range(0,len(self.process),row*column):
            print(num)
            for each in range(1,row*column+1): #控制每张子图展示图片数量
                if num+each-1<len(self.process):
                    img = self.process[num+each-1]
                    plt.subplot(row,column,each)
                    plt.imshow(img)
                else:
                    continue
            plt.show()
   
    def save(self,filepath):  #保存图片到指定路径
        for num in range(len(self.process)):
            img = self.process[num]
            img.save(filepath+'{}'.format(num)+self.layout)
            
class TestImageShop: #测试类
    def __init__(self,layout,file,lis,process):
        self.T = ImageShop(layout,file,lis,process)

    def batch(self,operation):
        self.T.batch_ps(operation,'Blur')
    def save(self,filepath):
        self.T.save(filepath)
    def display(self):
        self.T.display()

image = 'H:\\图片\\'  
parameters = [640,480]
file = 'F:\慕课学习资源\Tensorflow-MOOC-main\Animals Dataset\\test'   #图片集路径
layout = '.png'
lis,process = [],[]
operation = 'Resize'
filepath = 'E:\coding\code_design\week7\\'

test = TestImageShop(layout,file,lis,process)
test.batch(operation)
test.save(filepath)
test.display()

batch_ps函数完成批量处理图片的操作,此处我们使用ResizeBlur两种操作进行测试。

原始图片是一个深度学习测试集,图片大小不一。通过batch_ps函数统一进行大小放缩和模糊操作。save函数进行保存操作,输出到指定路径。display函数进行展示,因为使用subplot函数同时展示多张图表,并提供参数来修改最多展示的图片数。


由此可见图片的大小放缩一致。
同一张图处理前:

模糊操作处理后:

边缘提取之后的图片:


上一篇下一篇

猜你喜欢

热点阅读