当前位置: 首页 > news >正文

自制简单的图片查看器(python)

图片格式:支持常见的图片格式(JPG、PNG、BMP、GIF)。

import os
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTkclass ImageViewer:def __init__(self, root):self.root = rootself.root.title("图片查看器")self.root.geometry("800x600")self.root.configure(bg="#2E3440")  # 设置背景颜色# 当前图片路径self.current_image_path = Noneself.image = Noneself.photo = Noneself.scale_factor = 1.0self.is_auto_scaling = True  # 是否自动缩放# 创建界面self.create_widgets()def create_widgets(self):"""创建界面组件"""# 顶部工具栏toolbar = tk.Frame(self.root, bg="#3B4252")  # 工具栏背景颜色toolbar.pack(side=tk.TOP, fill=tk.X)# 打开按钮btn_open = tk.Button(toolbar, text="打开", command=self.open_image, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_open.pack(side=tk.LEFT, padx=5, pady=5)# 放大按钮btn_zoom_in = tk.Button(toolbar, text="放大", command=self.zoom_in, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_zoom_in.pack(side=tk.LEFT, padx=5, pady=5)# 缩小按钮btn_zoom_out = tk.Button(toolbar, text="缩小", command=self.zoom_out, bg="#81A1C1", fg="white", activebackground="#5E81AC", activeforeground="white", font=("微软雅黑", 12))btn_zoom_out.pack(side=tk.LEFT, padx=5, pady=5)# 图片显示区域self.canvas = tk.Canvas(self.root, bg="#2E3440", highlightthickness=0)self.canvas.pack(fill=tk.BOTH, expand=True)# 绑定窗口大小变化事件self.root.bind("<Configure>", self.on_window_resize)def open_image(self):"""打开图片"""file_path = filedialog.askopenfilename(title="选择图片",filetypes=[("图片文件", "*.jpg *.jpeg *.png *.bmp *.gif")])if file_path:self.current_image_path = file_pathself.load_image()def load_image(self):"""加载图片"""try:self.image = Image.open(self.current_image_path)self.scale_factor = 1.0self.is_auto_scaling = True  # 加载图片时启用自动缩放self.update_image()except Exception as e:messagebox.showerror("错误", f"无法加载图片: {str(e)}")def update_image(self):"""更新显示的图片"""if self.image:# 计算缩放后的尺寸canvas_width = self.canvas.winfo_width()canvas_height = self.canvas.winfo_height()image_width, image_height = self.image.sizeif self.is_auto_scaling:# 自动缩放时计算缩放比例width_ratio = canvas_width / image_widthheight_ratio = canvas_height / image_heightself.scale_factor = min(width_ratio, height_ratio)# 缩放图片width = int(image_width * self.scale_factor)height = int(image_height * self.scale_factor)resized_image = self.image.resize((width, height), Image.Resampling.LANCZOS)self.photo = ImageTk.PhotoImage(resized_image)# 清除画布并显示图片self.canvas.delete("all")self.canvas.create_image(canvas_width // 2,canvas_height // 2,anchor=tk.CENTER,image=self.photo)def zoom_in(self):"""放大图片"""if self.image:self.is_auto_scaling = False  # 手动缩放时禁用自动缩放self.scale_factor *= 1.2self.update_image()def zoom_out(self):"""缩小图片"""if self.image:self.is_auto_scaling = False  # 手动缩放时禁用自动缩放self.scale_factor /= 1.2self.update_image()def on_window_resize(self, event):"""窗口大小变化时自动调整图片大小"""if self.image and self.is_auto_scaling:self.update_image()if __name__ == "__main__":root = tk.Tk()app = ImageViewer(root)root.mainloop() 

http://www.lryc.cn/news/540662.html

相关文章:

  • RD-搭建测试环境
  • 从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
  • 【深度学习】使用飞桨paddle实现波士顿房价预测任务
  • 钉钉多维表:数据管理与协作的新篇章
  • 高级推理的多样化推理与验证
  • 深入理解 MySQL 8 C++ 源码:SELECT MOD(MONTH(NOW()), 2) 的函数执行过程
  • 【算法系列】leetcode1419 数青蛙 --模拟
  • 蓝桥杯 Java B 组之背包问题、最长递增子序列(LIS)
  • Git如何将一个分支的内容同步到另一个分支
  • [C#]C# winform部署yolov12目标检测的onnx模型
  • 51c大模型~合集69
  • 2025寒假周报4
  • 自学Java-AI结合GUI开发一个石头迷阵的游戏
  • buuctf-[极客大挑战 2019]Knife题解
  • Spring MVC 对象转换器:初级开发者入门指南
  • 语音直播交友app出海:语音直播交友系统软件源码搭建国际化发展技术层面分析
  • Web Scraper,强大的浏览器爬虫插件!
  • EasyRTC:基于WebRTC与P2P技术,开启智能硬件音视频交互的全新时代
  • go 定时任务 gocron timer
  • uniapp引入uview组件库(可以引用多个组件)
  • MySQL主从架构
  • 科普mfc100.dll丢失怎么办?有没有简单的方法修复mfc100.dll文件
  • 论文笔记:How Much Can Time-related Features Enhance Time Series Forecasting?
  • Qt学习(六) 软件启动界面 ,注册表使用 ,QT绘图, 视图和窗口绘图,Graphics View绘图框架:简易CAD
  • JavaScript系列(80)--WebAssembly 基础入门
  • 蓝桥杯刷题2.21|笔记
  • 053 性能压测 单机锁 setnx
  • 【天线】IFA天线知识点摘抄
  • Mysql视图有什么作用?你是否使用过视图?
  • 【vue项目如何利用event-stream实现文字流式输出效果】