目录
- 效果展示
- 代码
- 脚本代码
效果展示
锐化前:
锐化后
代码
sharpen_img.py
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk,ImageFilter
import os
class ImageViewerApp:
def __init__(self, root):
self.root = root
self.root.title("图片查看器")
self.suffix = ''
self.dirname_filename =''
self.image_label = None
self.new_window = None
# 设置按钮用于打开文件对话框
self.open_button = tk.Button(self.root, text="打开图片", command=self.open_image)
self.open_button.pack(pady=20)
def on_window_close(self,file_path):
# 检查文件是否存在
if os.path.exists(file_path):
# 删除文件
os.remove(file_path)
print(f"原图已删除。")
else:
print(f"文件 {file_path} 不存在。")
self.new_window.destroy()
def on_escape(self,event):
root.destroy()
def resize_img(self,original_image):
# 获取屏幕的长和宽
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 获取图片的长和宽
image_width, image_height = original_image.size
# 检查图片是否超出了屏幕大小,并相应地调整大小
if image_width > screen_width-50 or image_height > screen_height-50:
# 计算缩放比例,这里我们取屏幕和图片尺寸中较小的那个比例的70%
scale_factor = min(screen_width / image_width, screen_height / image_height) * 0.8
new_width = int(image_width * scale_factor)
new_height = int(image_height * scale_factor)
# 缩放图片
resized_image = original_image.resize((new_width, new_height), Image.ANTIALIAS)
# 转换为Tkinter可以显示的格式
photo = ImageTk.PhotoImage(resized_image)
else:
# 图片不需要缩放,直接转换
photo = ImageTk.PhotoImage(original_image)
return photo
def update_image(self,factor, input_image_path):
"""根据滑块的值更新图片"""
global photo, original_image
sharpened = self.sharpen_image(input_image_path, factor)
# 对锐化后的图片缩放以使它小于屏幕大小
photo = self.resize_img(sharpened)
self.image_label.config(image=photo)
self.image_label.image = photo # 保持对photo的引用
def next_window(self,file_path):
# 创建一个新的窗口
self.new_window = tk.Toplevel(root)
self.new_window.title("图片锐化")
# 绑定Esc键关闭窗口
self.new_window.bind("<Escape>", self.on_escape)
# 这样写会立即执行 on_window_close,直接把图片删除
# self.new_window.protocol("WM_DELETE_WINDOW", self.on_window_close(file_path))
self.new_window.protocol("WM_DELETE_WINDOW", lambda: self.on_window_close(file_path))
self.suffix = file_path.split('.')[-1]
self.dirname_filename = file_path.split('.')[0]
# 打开图片##########################################################
original_image = Image.open(file_path)
# 输入打开后的图片流 对图片进行resize,然后返回photo
photo = self.resize_img(original_image)
# 显示图片
self.image_label = tk.Label(self.new_window)
self.image_label.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.image_label.config(image=photo)
self.image_label.image = photo # 保持对photo的引用
# 滑块,用于调整锐化级别
slider = tk.Scale(self.new_window, from_=0.0, to=10.0, orient="horizontal",
command=lambda value: self.update_image(slider.get(), file_path))
slider.pack(side=tk.BOTTOM, fill=tk.X, pady=10)
def sharpen_image(self,input_image_path, factor):
img = Image.open(input_image_path)
sharpened_img = img.filter(ImageFilter.UnsharpMask(radius=factor, threshold=3)) # 注意:这里假设我们只用radius和threshold
output_image_path = self.dirname_filename + f'_new.' + self.suffix
sharpened_img.save(output_image_path)
return sharpened_img
def open_image(self):
# 打开文件对话框,选择图片
file_path = filedialog.askopenfilename(filetypes=[("图片文件", "*.png;*.jpg;*.jpeg;*.gif;*.bmp")])
if file_path:
# 打开新窗口
self.next_window(file_path)
if __name__ == "__main__":
root = tk.Tk()
app = ImageViewerApp(root)
root.mainloop()
脚本代码
sharpen_img.bat
@echo on
chcp 65001
E:
cd E:\pythonProject\ImageTools\sharpen_image
call H:\Programmer_Software\anaconda2022\Scripts\activate base
python v3_sharpen_image.py
exit()