这里写自定义目录标题
如何利用python简单的实现图片放大的方法,我列举几种!
第一种:
Tkinter库编写的简单GUI应用程序,用于调整图像大小。程序主要功能如下:
创建一个主窗口,并将其标题设置为"**Image Resizer**"。
在窗口中添加了两个标签和输入框:第一个标签是"Input Image Path:",对应的输入框用来让用户输入或选择要调整大小的图片文件路径;第二个标签是"Output Folder Path:",对应的输入框让用户指定输出图片的文件夹路径。
添加了三个按钮:
“Browse”按钮(对应open_file_dialog函数):点击后会打开文件对话框,用户可以选择一个图片文件,所选文件的路径会被填入到"Input Image Path:"的输入框中。
另一个“Browse”按钮(对应open_output_dialog函数):点击后会打开目录对话框,用户可以选取一个输出文件夹,该文件夹路径会被填入到"Output Folder Path:"的输入框中。
“Process Image”按钮(对应process_image函数):当用户在两个输入框中分别选择了输入图片和输出文件夹后,点击此按钮将会执行图像处理操作。具体操作包括:打开输入图片,将图片尺寸放大一倍(宽度和高度均翻倍),然后将放大的图片保存到指定的输出文件夹中,文件名为'scaled_image.png'。
最后,使用root.mainloop()启动程序的主循环,等待用户与界面进行交互。
代码如下:
import tkinter as tk
from tkinter import filedialog
from PIL import Image
def open_file_dialog():
file_path = filedialog.askopenfilename()
input_path_entry.delete(0, tk.END)
input_path_entry.insert(0, file_path)
def open_output_dialog():
output_path = filedialog.askdirectory()
output_path_entry.delete(0, tk.END)
output_path_entry.insert(0, output_path)
def process_image():
input_path = input_path_entry.get()
output_path = output_path_entry.get()
if input_path and output_path:
image = Image.open(input_path)
scaled_image = image.resize((image.width * 2, image.height * 2))
scaled_image.save(output_path + '/scaled_image.png')
root = tk.Tk()
root.title("Image Resizer")
input_path_label = tk.Label(root, text="Input Image Path:")
input_path_label.grid(row=0, column=0)
input_path_entry = tk.Entry(root, width=50)
input_path_entry.grid(row=0, column=1)
browse_input_button = tk.Button(root, text="Browse", command=open_file_dialog)
browse_input_button.grid(row=0, column=2)
output_path_label = tk.Label(root, text="Output Folder Path:")
output_path_label.grid(row=1, column=0)
output_path_entry = tk.Entry(root, width=50)
output_path_entry.grid(row=1, column=1)
browse_output_button = tk.Button(root, text="Browse", command=open_output_dialog)
browse_output_button.grid(row=1, column=2)
process_button = tk.Button(root, text="Process Image", command=process_image)
process_button.grid(row=2, column=1)
root.mainloop()
运行结果如下:
第一行导入图片路径,第二行导出路径,第三行直接是执行放大,我这里是放大2倍,当然也可以放大N倍哦!