网站建设的颜色值,宿迁房产网58同城网二手房,分类信息网站 建议 建设,百度怎么发布店铺信息Python实现GUI图片浏览程序 下面程序需要pillow库。pillow是 Python 的第三方图像处理库#xff0c;需要安装才能实用。pillow是PIL#xff08; Python Imaging Library#xff09;基础上发展起来的#xff0c;需要注意的是pillow库安装用pip install pillow#xff0c;导…Python实现GUI图片浏览程序 下面程序需要pillow库。pillow是 Python 的第三方图像处理库需要安装才能实用。pillow是PIL Python Imaging Library基础上发展起来的需要注意的是pillow库安装用pip install pillow导包时要用PIL来导入。更多情况可见https://blog.csdn.net/cnds123/article/details/126141838 一、简单的图片查看程序
功能使用了tkinter库来创建一个窗口用户可以通过该窗口选择一张图片并在窗口中显示。能调整窗口大小以适应图片。效果图如下 源码如下
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk# 创建一个Tkinter窗口
root tk.Tk()
root.geometry(400x300) # 设置宽度为400像素高度为300像素
root.title(Image Viewer)# 添加一个按钮来选择图片
def open_image():try:file_path filedialog.askopenfilename()if file_path:image Image.open(file_path)photo ImageTk.PhotoImage(image)# 清除旧图片for widget in root.winfo_children():if isinstance(widget, tk.Label):widget.destroy()label tk.Label(root, imagephoto)label.image photolabel.pack()# 调整窗口大小以适应图片root.geometry({}x{}.format(image.width, image.height))except AttributeError:print(No image selected.)button tk.Button(root, textOpen Image, commandopen_image)
button.pack()# 运行窗口
root.mainloop()此程序创建一个tkinter窗口设置窗口的大小为400x300像素并设置窗口标题为Image Viewer。
添加一个按钮当用户点击该按钮时会弹出文件选择对话框用户可以选择一张图片文件。
选择图片后程序会使用PIL库中的Image.open方法打开所选的图片文件并将其显示在窗口中。
程序会在窗口中显示所选的图片并在用户选择新图片时清除旧图片。
示例中使用try-except块来捕获FileNotFoundError该错误会在用户取消选择图片时触发。当用户取消选择图片时会打印一条消息提示用户没有选择图片。这样就可以避免因为取消选择图片而导致的报错。 二、图片查看程序1
“Open Directory”按钮用于指定一个目录窗体上再添加两个按钮“Previous Image” 和“Next Image”单击这两个按钮实现切换显示指定目录中的图片。这三个按钮水平排列在顶部在下方显示图片。如果所选图片的尺寸超过了窗口的大小程序会将图片缩放到合适的尺寸以适应窗口。效果图如下 源码如下
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import osclass ImageViewer:def __init__(self, root):self.root rootself.root.geometry(400x350)self.root.title(Image Viewer)self.image_dir self.image_files []self.current_index 0# 创建顶部按钮框架self.button_frame tk.Frame(self.root)self.button_frame.pack(sidetop)# 创建打开目录按钮self.open_button tk.Button(self.button_frame, textOpen Directory, commandself.open_directory)self.open_button.pack(sideleft)# 创建上一张图片按钮self.prev_button tk.Button(self.button_frame, textPrevious Image, commandself.show_previous_image)self.prev_button.pack(sideleft)# 创建下一张图片按钮self.next_button tk.Button(self.button_frame, textNext Image, commandself.show_next_image)self.next_button.pack(sideleft)# 创建图片显示区域self.image_label tk.Label(self.root)self.image_label.pack()def open_directory(self):try:self.image_dir filedialog.askdirectory()if self.image_dir:self.image_files [f for f in os.listdir(self.image_dir) if f.endswith(.jpg) or f.endswith(.png) or f.endswith(.jfif)]self.current_index 0self.show_image()except tk.TclError:print(No directory selected.)def show_image(self):if self.image_files:image_path os.path.join(self.image_dir, self.image_files[self.current_index])image Image.open(image_path)image.thumbnail((400, 300), Image.ANTIALIAS)photo ImageTk.PhotoImage(image)self.image_label.config(imagephoto)self.image_label.image photodef show_previous_image(self):if self.image_dir:if self.image_files:self.current_index (self.current_index - 1) % len(self.image_files)self.show_image()else:print(Please open a directory first.)else:print(Please open a directory first.)def show_next_image(self):if self.image_dir:if self.image_files:self.current_index (self.current_index 1) % len(self.image_files)self.show_image()else:print(Please open a directory first.)else:print(Please open a directory first.)root tk.Tk()
app ImageViewer(root)
root.mainloop()三、图片查看程序2
窗体上有3个控件列表框和按钮和在窗体上左侧上下放置右侧区域显示图片 “Open Directory”按钮用于指定目录中列表用于放置指定目录中的所有图片文件名点击列表中的图片文件名图片在右侧不变形缩放显示到窗体上图片缩放到合适的尺寸以适应窗口效果图如下 源码如下
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import os# 创建主窗口
root tk.Tk()
root.geometry(600x300)
root.title(Image Viewer)# 创建一个Frame来包含按钮和列表框
left_frame tk.Frame(root)
left_frame.pack(sidetk.LEFT, filltk.BOTH, padx5, pady5)# 创建一个Frame来包含图片显示区域
right_frame tk.Frame(root)
right_frame.pack(sidetk.RIGHT, filltk.BOTH, expandTrue)# 创建一个列表框来显示文件名
listbox tk.Listbox(left_frame)
listbox.pack(filltk.BOTH, expandTrue)# 创建一个滚动条并将其与列表框关联
scrollbar tk.Scrollbar(root, orienttk.VERTICAL)
scrollbar.pack(sidetk.RIGHT, filltk.Y)
scrollbar.config(commandlistbox.yview)
listbox.config(yscrollcommandscrollbar.set)# 创建一个标签来显示图片
image_label tk.Label(right_frame)
image_label.pack(filltk.BOTH, expandTrue)# 函数打开目录并列出图片文件
def open_directory():directory filedialog.askdirectory()if directory:# 清空列表框listbox.delete(0, tk.END)# 列出目录中的所有图片文件for file in os.listdir(directory):if file.lower().endswith((.jpg, .jpeg, .png, .gif,.jfif)):listbox.insert(tk.END, file)# 保存当前目录open_directory.current_directory directory# 函数在右侧显示选中的图片
def show_selected_image(event):if not hasattr(open_directory, current_directory):return# 获取选中的文件名selected_file listbox.get(listbox.curselection())# 构建完整的文件路径file_path os.path.join(open_directory.current_directory, selected_file)# 打开图片并进行缩放image Image.open(file_path)image.thumbnail((right_frame.winfo_width(), right_frame.winfo_height()), Image.ANTIALIAS)# 用PIL的PhotoImage显示图片photo ImageTk.PhotoImage(image)image_label.config(imagephoto)image_label.image photo # 保存引用防止被垃圾回收# 创建“Open Directory”按钮
open_button tk.Button(left_frame, textOpen Directory, commandopen_directory)
open_button.pack(filltk.X)# 绑定列表框选择事件
listbox.bind(ListboxSelect, show_selected_image)# 运行主循环
root.mainloop()OK!