1. 实现基类 Filter,至少包括两个数据属性,一个属性是待处理的图片实例,即 PIL 库的
    Image 实例,另一个是参数列表,用以存储可能使用参数的滤波器的参数;至少包括一个
    方法属性,即 filter()方法,能够对 Image 实例的特定处理。但在该类中并不需要进行实
    现,其实现细节应该交给子类。
  2. 实现 Filter 类的多个子类,分别实现对图片的一些滤波处理,至少应进行边缘提取,锐
    化,模糊及大小调整四类操作,也即应实现至少 4 个子类,分别对基类中的 filter()方法进
    行实现。注意,并不需要真正实现对应的操作,可简单地通过 PIL 中的 Image 和
    ImageFilter 模块来实现。具体可参见
    https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html。
  3. 实现类 ImageShop,其至少包含四个数据属性,分别是图片格式,图片文件(应该支持
    目录),存储图片实例(Image 实例)的列表以及存储处理过的图片(如果需要的话)。至
    少包含如下方法属性,分别是从路径加载特定格式的图片(load_images(),应加载文件或
    目录中的所有特定格式图片);处理图片的内部方法__batch_ps(Filter),利有某个过滤器对
    所有图片进行处理;批量处理图片的对外公开方法(batch_ps()),注意该方法要至少有
    一个操作参数,且该参数可以不定长,即可以同时进行若干操作(如调整大小并模糊
    等),其参数可定义成一种特定格式的 tuple 输入,比如(操作名,参数),根据操作名
    生成对应的 Filter 子类并调用 __batch_ps 来完成批处理;处理效果显示(display()),
    注意该方法应该有参数,如考虑多图片呈现可能需要行,列以及每张图片的大小,以及最
    多显示多少张等,可通过 matplotlib 中的 subplot 等来实现;处理结果输出(save()),该
    方法应该有输出路径或输出格式等参数。
  4. 实现测试类 TestImageShop,对该类进行测试,指定图片路径,指定要进行的操作(如
    有参数也可应提供),并对执行结果进行呈现和存储。

一、各模块说明

1.导入有关库

import os
import glob
from PIL import Image,ImageFilter
import matplotlib.pyplot as plt

2.建立一个基类Filter,包括两个数据属性

class Filter:
    '''这是我们定义的父类'''
    def __init__(self,image,parameter):
        self.image = image  #图片实例
        self.parameter = parameter  #参数
    def filter(): #具体实现在之后定义的子类中实现
        pass

3.实现 Filter 类的多个子类,分别实现对图片的一些滤波处理

class Edge_extraction(Filter):
    '''这是我们定义的边缘提取类'''
    def __init__(self, image, parameter):
        super(Edge_extraction,self).__init__(image,parameter)  #进行参数继承
    def filter(self,picture):   #picture是一个图片实例
        picture = picture.filter(ImageFilter.FIND_EDGES) #有一个新参数为picture
        return picture
        
class Sharpen(Filter):
    '''这是我们定义的锐化类'''
    def __init__(self, image, parameter):
        super(Sharpen,self).__init__(image,parameter)
    def filter(self,picture):   #picture是一个图片实例
        picture = picture.filter(ImageFilter.SHARPEN)  #有一个新参数为picture
        return picture
    
class Dim(Filter):
    '''这是我们定义的模糊类'''
    def __init__(self, image, parameter):
        super(Dim,self).__init__(image,parameter)
    def filter(self,picture):   #picture是一个图片实例
        picture = picture.filter(ImageFilter.BLUR)  #有一个新参数为picture
        return picture
    
class Resize(Filter):
    '''这是我们定义的调整大小类'''
    def __init__(self, image, parameter):
        super(Edge_extraction,self).__init__(image,parameter)
    def filter(self,picture):   #picture是一个图片实例
        picture = picture.resize(self.parameter[0],self.parameter[1])  #有一个新参数为picture
        return picture

4.实现类 ImageShop,包含四个数据属性

class ImageShop:
    '''批量处理图片的类'''
    def __init__(self,pic_format,path,picture_list,process,parameter): 
        '''初始化,传入三个属性'''
        self.pic_format = pic_format  #pic_format 图片格式后缀
        self.path = path           #path 图片集合路径
        self.picture_list = picture_list  #image_list Image 实例
        self.process = process  #处理过的图片
        self.parameter = parameter
    def load_images(self):
        '''将特定格式下的某一文件夹下所有文件路径加入‘path’'''
        self.path = glob.glob(os.path.join(self.path,'*'+self.pic_format))
    def __batch_ps(self,Filter,num):  #传入的为一个类
        '''批量处理图片的内部方法'''
        self.process[num] = Filter.filter(self.process[num])
    def batch_ps(self,operation,*args):
        '''批量处理图片的外部方法'''
        ImageShop.load_images(self)  #对self.path进行更新
        
        for i in self.picture_list:
            self.process.append(Image.open(i))  #对picture_list中的进行open加入process
            
        for i in range(len(self.process)):
            filter1 = eval(operation)(self.process[i],self.parameter)
            ImageShop.__batch_ps(self,filter1, i)
            
            for j in range(0,len(args),2):
                filter2 = eval(args[j])(self.process[i],args[j+1])
                ImageShop.__batch_ps(self,filter2,i)
    def display(self,row=3,column=4,maximum=10): #利用subplot函数批量显示处理后图片
        if len(self.process)>maximum:
            self.process = self.process[:maximum]  #控制最大显示图片数
        for i in range(0,len(self.process),row*column):
            
            for j in range(1,row*column+1): #控制每张子图展示图片数量
                if i+j-1<len(self.proess):
                    img = self.process[i+j-1]
                    
                    plt.subplot(row,column,j)
                    plt.imshow(img)
                else:
                    continue
            plt.show()  #将图片展示出来
    def save(self,savepath):  #保存图片到指定路径
        for i in range(len(self.process)):
            img = self.process[i]
            img.save(savepath+'{}'.format(i)+self.format)

5.运行代码

parameter = [400,400]  #将图片变成方形
path = 'D:/学习文件/大三上/现代程序设计/第六次作业/picture' #图片集路径
pic_format = '.jpg'
picture_list,process = [],[]
operation = 'Resize'
savepath = 'D:/学习文件/大三上/现代程序设计/第六次作业/picture/new1'
#下面开始测试,用test1表示测试类
test1 = TestImageShop(pic_format,path,picture_list,process,parameter)
test1.batch(operation,'Dim',[400,400])
test1.save(savepath)
test1.display()

二、运行结果说明

我们执行了’Resize’,’Dim’的操作,就是把图片变成方形并模糊

原始图片如下所示

在这里插入图片描述

在这里插入图片描述

处理之后

在这里插入图片描述
在这里插入图片描述

三、一些思考(附加)

1.相似性

两张图片之间的相似性可以通过他们的哈希值之间的汉明距离来判断,汉明距离越小则说明图片越相似

在这里插入图片描述

2.深度卷积网络以及其在计算机视觉中的应用现状

人脸识别

图像分割

AI绘画

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐