import os
import win32com.client
import tkinter as tk
from tkinter import filedialog
# create a Photoshop.Application object
psApp = win32com.client.Dispatch("Photoshop.Application")
# create a tkinter GUI window
root = tk.Tk()
root.title("Image Resizer")
# create global variables for user inputs
filename = ""
width = ""
height = ""
# callback function for "Browse" button
def browse_file():
global filename
filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image", filetypes=(("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("All files", "*.*")))
if filename != "":
file_label.config(text=filename)
# callback function for "Resize" button
def resize_image():
global width, height
width = width_entry.get()
height = height_entry.get()
if filename == "":
tk.messagebox.showerror("Error", "Please select an image file.")
elif not width.isdigit() or not height.isdigit():
tk.messagebox.showerror("Error", "Please enter valid dimensions.")
else:
# open the selected image file in Photoshop
doc = psApp.Open(filename)
# resize the image to the specified dimensions
doc.ResizeImage(int(width), int(height))
# save the resized image with a new filename
new_filename = os.path.splitext(filename)[0] + "_" + width + "x" + height + os.path.splitext(filename)[1]
doc.SaveAs(new_filename)
doc.Close()
tk.messagebox.showinfo("Success", "Image resized and saved as\n" + new_filename)
# create GUI widgets
file_label = tk.Label(root, text="No file selected.")
file_label.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
browse_button = tk.Button(root, text="Browse", command=browse_file)
browse_button.grid(row=1, column=0, padx=10, pady=10)
width_label = tk.Label(root, text="Width:")
width_label.grid(row=2, column=0, padx=10, pady=10)
width_entry = tk.Entry(root)
width_entry.grid(row=2, column=1, padx=10, pady=10)
height_label = tk.Label(root, text="Height:")
height_label.grid(row=3, column=0, padx=10, pady=10)
height_entry = tk.Entry(root)
height_entry.grid(row=3, column=1, padx=10, pady=10)
resize_button = tk.Button(root, text="Resize", command=resize_image)
resize_button.grid(row=4, column=1, padx=10, pady=10)
root.grid_columnconfigure(2, weight=1)
# run the GUI loop
root.mainloop()