使用Python實(shí)現(xiàn)一個(gè)圖片查看器
效果圖

圖片格式:支持常見的圖片格式(JPG、PNG、BMP、GIF)。
完整代碼
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from PIL import Image, ImageTk
class ImageViewer:
def __init__(self, root):
self.root = root
self.root.title("圖片查看器")
self.root.geometry("800x600")
self.root.configure(bg="#2E3440") # 設(shè)置背景顏色
# 當(dāng)前圖片路徑
self.current_image_path = None
self.image = None
self.photo = None
self.scale_factor = 1.0
self.is_auto_scaling = True # 是否自動(dòng)縮放
# 創(chuàng)建界面
self.create_widgets()
def create_widgets(self):
"""創(chuàng)建界面組件"""
# 頂部工具欄
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)
# 圖片顯示區(qū)域
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_path
self.load_image()
def load_image(self):
"""加載圖片"""
try:
self.image = Image.open(self.current_image_path)
self.scale_factor = 1.0
self.is_auto_scaling = True # 加載圖片時(shí)啟用自動(dòng)縮放
self.update_image()
except Exception as e:
messagebox.showerror("錯(cuò)誤", f"無法加載圖片: {str(e)}")
def update_image(self):
"""更新顯示的圖片"""
if self.image:
# 計(jì)算縮放后的尺寸
canvas_width = self.canvas.winfo_width()
canvas_height = self.canvas.winfo_height()
image_width, image_height = self.image.size
if self.is_auto_scaling:
# 自動(dòng)縮放時(shí)計(jì)算縮放比例
width_ratio = canvas_width / image_width
height_ratio = canvas_height / image_height
self.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 # 手動(dòng)縮放時(shí)禁用自動(dòng)縮放
self.scale_factor *= 1.2
self.update_image()
def zoom_out(self):
"""縮小圖片"""
if self.image:
self.is_auto_scaling = False # 手動(dòng)縮放時(shí)禁用自動(dòng)縮放
self.scale_factor /= 1.2
self.update_image()
def on_window_resize(self, event):
"""窗口大小變化時(shí)自動(dòng)調(diào)整圖片大小"""
if self.image and self.is_auto_scaling:
self.update_image()
if __name__ == "__main__":
root = tk.Tk()
app = ImageViewer(root)
root.mainloop() 到此這篇關(guān)于使用Python實(shí)現(xiàn)一個(gè)圖片查看器的文章就介紹到這了,更多相關(guān)Python圖片查看器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python-can中Notifier類的實(shí)現(xiàn)報(bào)文的實(shí)時(shí)分發(fā)
文章介紹了利用python-can庫中的Notifier類創(chuàng)建一個(gè)線程來從總線讀取CAN消息,并通過回調(diào)函數(shù)將消息分發(fā)給監(jiān)聽對象,具有一定的參考價(jià)值,感興趣的可以了解一下2026-04-04
基于OpenCV實(shí)現(xiàn)小型的圖像數(shù)據(jù)庫檢索功能
下面就使用VLAD表示圖像,實(shí)現(xiàn)一個(gè)小型的圖像數(shù)據(jù)庫的檢索程序。下面實(shí)現(xiàn)需要的功能模塊,分步驟給大家介紹的非常詳細(xì),對OpenCV圖像數(shù)據(jù)庫檢索功能感興趣的朋友跟隨小編一起看看吧2021-12-12
Python使用pandas導(dǎo)入csv文件內(nèi)容的示例代碼
這篇文章主要介紹了Python使用pandas導(dǎo)入csv文件內(nèi)容,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
使用Python將PDF表格提取到文本,CSV和Excel文件中
本文將介紹如何使用簡單的Python代碼從PDF文檔中提取表格數(shù)據(jù)并將其寫入文本、CSV和Excel文件,從而輕松實(shí)現(xiàn)PDF表格的自動(dòng)化提取,有需要的可以參考下2024-11-11
Python中關(guān)于集合的介紹與常規(guī)操作解析
Python除了List、Tuple、Dict等常用數(shù)據(jù)類型外,還有一種數(shù)據(jù)類型叫做集合(set),集合的最大特點(diǎn)是:集合里邊的元素是不可重復(fù)的并且集合內(nèi)的元素還是無序的2021-09-09
基于Python+Dify實(shí)現(xiàn)批量OCR識別的自動(dòng)化解決方案
在日常工作中,我們經(jīng)常會遇到大量圖片格式的PDF文件,這類文件無法直接復(fù)制文本,手動(dòng)逐頁OCR識別效率極低,下面我們看看如何使用Python和Dify實(shí)現(xiàn)全自動(dòng)化解決這一問題吧2025-12-12
python多進(jìn)程(加入進(jìn)程池)操作常見案例
這篇文章主要介紹了python多進(jìn)程(加入進(jìn)程池)操作,結(jié)合常見案例形式分析了Python多進(jìn)程復(fù)制文件、加入進(jìn)程池及多進(jìn)程聊天等相關(guān)操作技巧,需要的朋友可以參考下2019-10-10
關(guān)于python中密碼加鹽的學(xué)習(xí)體會小結(jié)
這篇文章主要介紹了關(guān)于python中密碼加鹽的學(xué)習(xí)體會小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

