使用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"無(wú)法加載圖片: {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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python中字符串相關(guān)操作的運(yùn)算符總結(jié)
這篇文章主要介紹了Python之字符串相關(guān)操作的運(yùn)算符,本文通過(guò)案例給大家詳細(xì)講解python字符串運(yùn)算符的說(shuō)明,需要的朋友可以參考下2023-12-12mvc框架打造筆記之wsgi協(xié)議的優(yōu)缺點(diǎn)以及接口實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于mvc框架打造筆記之wsgi協(xié)議的優(yōu)缺點(diǎn)以及接口實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08使用jupyter notebook輸出顯示不完全的問(wèn)題及解決
這篇文章主要介紹了使用jupyter notebook輸出顯示不完全的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python中DJANGO簡(jiǎn)單測(cè)試實(shí)例
這篇文章主要介紹了Python中DJANGO簡(jiǎn)單測(cè)試,實(shí)例分析了DJANGO的用法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-05-05python @classmethod 的使用場(chǎng)合詳解
這篇文章主要介紹了python @classmethod 的使用場(chǎng)合詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08win10系統(tǒng)Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安裝教程
這篇文章主要介紹了win10系統(tǒng) Anaconda 和 Pycharm 的 Tensorflow2.0 之 CPU和 GPU 版本安裝教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12