python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python图片预览

python实现压缩包图片预览与收藏功能

作者:winfredzhang

在日常处理图片资料时,我们经常会遇到大量打包在压缩文件中的照片,逐个解压再查看不仅麻烦,而且浪费时间,下面我们来看看如何使用Python实现压缩包图片预览与收藏功能吧

在日常处理图片资料时,我们经常会遇到大量打包在压缩文件中的照片。逐个解压再查看不仅麻烦,而且浪费时间。如果能在一个小工具里直接浏览压缩包中的照片,并支持将常用的压缩文件收藏起来,岂不是非常高效?

本文将介绍一个基于 wxPython 的小工具,它可以:

功能设计

整个界面分为 左右两部分

左侧列表区

右侧预览区

预览选中的照片,支持缩放显示

交互逻辑:

关键实现

1. 压缩文件解压与图片筛选

解压功能统一由 extract_archive 处理,支持 zip/rar/tar 三种常见格式。

解压后会筛选出 .jpg/.png/.gif 等图片文件,显示在 current_photo_listbox 中。

def extract_archive(self, archive_path):
    """解压缩文件并列出其中的照片"""
    # 解压目录
    extract_dir = os.path.join(self.temp_dir, os.path.basename(archive_path))
    if os.path.exists(extract_dir):
        shutil.rmtree(extract_dir)
    os.makedirs(extract_dir)

    # 根据格式解压
    if archive_path.endswith(".zip"):
        with zipfile.ZipFile(archive_path, 'r') as zip_ref:
            zip_ref.extractall(extract_dir)
    elif archive_path.endswith(".rar"):
        with rarfile.RarFile(archive_path, 'r') as rar_ref:
            rar_ref.extractall(extract_dir)
    elif archive_path.endswith(".tar") or archive_path.endswith(".gz"):
        with tarfile.open(archive_path, 'r:*') as tar_ref:
            tar_ref.extractall(extract_dir)
    else:
        wx.CallAfter(self.status_text.SetLabel, "不支持的压缩格式")
        return

    # 列出照片
    photos = []
    for root, _, files in os.walk(extract_dir):
        for file in files:
            if file.lower().endswith((".png", ".jpg", ".jpeg", ".bmp", ".gif")):
                photos.append(os.path.join(root, file))

    wx.CallAfter(self.update_photo_list, photos)

2. 收藏功能

收藏按钮的逻辑是:获取当前 archive_listbox 选中的压缩文件路径,存储到 favorite_listbox 中。

def on_favorite(self, event):
    """收藏按钮事件"""
    selection = self.archive_listbox.GetSelection()
    if selection == wx.NOT_FOUND:
        wx.MessageBox("请先选择一个压缩文件", "提示", wx.OK | wx.ICON_INFORMATION)
        return

    archive_path = self.archive_listbox.GetClientData(selection)

    # 避免重复收藏
    for i in range(self.favorite_listbox.GetCount()):
        if self.favorite_listbox.GetClientData(i) == archive_path:
            wx.MessageBox("该压缩文件已经在收藏列表中", "提示", wx.OK | wx.ICON_INFORMATION)
            return

    display_text = os.path.basename(archive_path)
    self.favorite_listbox.Append(display_text, archive_path)
    self.status_text.SetLabel(f"已收藏: {display_text}")

3. 点击收藏项加载图片

收藏列表保存的是 压缩文件路径,当用户点击某个收藏项时,直接调用 extract_archive 来解压和加载照片。

def on_favorite_select(self, event):
    """收藏列表选择事件"""
    selection = self.favorite_listbox.GetSelection()
    if selection == wx.NOT_FOUND:
        return

    archive_path = self.favorite_listbox.GetClientData(selection)
    if not os.path.exists(archive_path):
        self.status_text.SetLabel(f"文件不存在: {archive_path}")
        wx.MessageBox(f"文件不存在: {archive_path}", "错误", wx.OK | wx.ICON_ERROR)
        return

    self.status_text.SetLabel(f"正在解压收藏文件: {os.path.basename(archive_path)}...")

    thread = threading.Thread(target=self.extract_archive, args=(archive_path,))
    thread.daemon = True
    thread.start()

这样用户只需点击收藏,就能快速打开常用的压缩包,无需再去文件夹里查找。

4. 图片预览

current_photo_listbox 选择某个图片时,右侧的 preview_panel 会用 wxPython 的 wx.StaticBitmap 显示缩略图:

def show_image(self, image_path):
    """显示图片"""
    image = wx.Image(image_path, wx.BITMAP_TYPE_ANY)
    W, H = self.preview_panel.GetClientSize()
    image = image.Scale(W, H, wx.IMAGE_QUALITY_HIGH)
    self.preview_bitmap.SetBitmap(wx.Bitmap(image))
    self.preview_panel.Refresh()

运行效果

整个操作流程非常直观,特别适合整理和浏览打包好的图片资源。

运行结果

到此这篇关于python实现压缩包图片预览与收藏功能的文章就介绍到这了,更多相关python图片预览内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文