python大作业图片处理开发01

项目结构:

filters.py:

from PIL import Image, ImageFilter, ImageOps
import numpy as np

class Filters:
    @staticmethod
    def apply_blur(image, radius=2):
        """应用模糊滤镜"""
        return image.filter(ImageFilter.GaussianBlur(radius=radius))
        
    @staticmethod
    def apply_sharpen(image):
        """应用锐化滤镜"""
        return image.filter(ImageFilter.SHARPEN)
        
    @staticmethod
    def apply_edge_enhance(image):
        """应用边缘增强滤镜"""
        return image.filter(ImageFilter.EDGE_ENHANCE)
        
    @staticmethod
    def apply_find_edges(image):
        """应用边缘检测滤镜"""
        return image.filter(ImageFilter.FIND_EDGES)
        
    @staticmethod
    def apply_emboss(image):
        """应用浮雕滤镜"""
        return image.filter(ImageFilter.EMBOSS)
        
    @staticmethod
    def apply_grayscale(image):
        """应用灰度滤镜"""
        return ImageOps.grayscale(image)
        
    @staticmethod
    def apply_sepia(image):
        """应用棕褐色滤镜"""
        sepia_matrix = (
            0.393, 0.769, 0.189, 0,
            0.349, 0.686, 0.168, 0,
            0.272, 0.534, 0.131, 0,
            0, 0, 0, 1
        )
        return image.convert('RGB').convert('RGB', sepia_matrix)
        
    @staticmethod
    def apply_invert(image):
        """应用反色滤镜"""
        return ImageOps.invert(image)
        
    @staticmethod
    def apply_solarize(image, threshold=128):
        """应用曝光滤镜"""
        return ImageOps.solarize(image, threshold=threshold)

image_processor.py:

from PIL import Image, ImageEnhance
import numpy as np

class ImageProcessor:
    def __init__(self):
        self.image = None
        self.original_image = None
        
    def load_image(self, path):
        """加载图像文件"""
        try:
            self.image = Image.open(path)
            self.original_image = self.image.copy()
            return True
        except Exception as e:
            print(f"加载图像时出错: {e}")
            return False
            
    def save_image(self, path):
        """保存图像到指定路径"""
        if self.image:
            try:
                self.image.save(path)
                return True
            except Exception as e:
                print(f"保存图像时出错: {e}")
                return False
        return False
        
    def reset_image(self):
        """重置图像到原始状态"""
        if self.original_image:
            self.image = self.original_image.copy()
            return True
        return False
        
    def resize_image(self, width, height):
        """调整图像大小"""
        if self.image:
            try:
                self.image = self.image.resize((width, height))
                return True
            except Exception as e:
                print(f"调整图像大小时出错: {e}")
                return False
        return False
        
    def crop_image(self, left, top, right, bottom):
        """裁剪图像"""
        if self.image:
            try:
                self.image = self.image.crop((left, top, right, bottom))
                return True
            except Exception as e:
                print(f"裁剪图像时出错: {e}")
                return False
        return False
        
    def rotate_image(self, angle):
        """旋转图像"""
        if self.image:
            try:
                self.image = self.image.rotate(angle)
                return True
            except Exception as e:
                print(f"旋转图像时出错: {e}")
                return False
        return False
        
    def adjust_brightness(self, factor):
        """调整亮度"""
        if self.image:
            try:
                enhancer = ImageEnhance.Brightness(self.image)
                self.image = enhancer.enhance(factor)
                return True
            except Exception as e:
                print(f"调整亮度时出错: {e}")
                return False
        return False
        
    def adjust_contrast(self, factor):
        """调整对比度"""
        if self.image:
            try:
                enhancer = ImageEnhance.Contrast(self.image)
                self.image = enhancer.enhance(factor)
                return True
            except Exception as e:
                print(f"调整对比度时出错: {e}")
                return False
        return False

file_handler.py:

import os
from PIL import Image

class FileHandler:
    @staticmethod
    def get_supported_formats():
        """获取支持的图像格式列表"""
        return [
            "jpg", "jpeg", "png", "bmp", "gif", "tiff", "webp"
        ]
        
    @staticmethod
    def is_supported_format(file_path):
        """检查文件是否为支持的图像格式"""
        if not os.path.isfile(file_path):
            return False
            
        extension = os.path.splitext(file_path)[1].lower().replace('.', '')
        return extension in FileHandler.get_supported_formats()
        
    @staticmethod
    def get_format_description():
        """获取格式描述,用于文件对话框"""
        formats = FileHandler.get_supported_formats()
        extensions = " ".join([f"*.{fmt}" for fmt in formats])
        return f"图像文件 ({extensions});;所有文件 (*)"
        
    @staticmethod
    def get_save_format_description():
        """获取保存格式描述,用于保存文件对话框"""
        formats = [
            ("JPG 文件", "*.jpg"),
            ("PNG 文件", "*.png"),
            ("BMP 文件", "*.bmp"),
            ("GIF 文件", "*.gif"),
            ("TIFF 文件", "*.tiff"),
            ("WebP 文件", "*.webp")
        ]
        
        return ";;".join([f"{desc} ({ext})" for desc, ext in formats])
posted @ 2025-05-12 20:26  vivi_vimi  阅读(32)  评论(0)    收藏  举报