Give me a complete code of program written in python language that automates image resizing in adobe photoshop using Python Photoshop API with Graphical User Interface that allows user input of dimension, filename, and save button
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()
Here's an example code that uses the Python Photoshop API to automate image resizing in Adobe Photoshop with a GUI that allows user input of dimensions, filename, and a save button:
Code: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()
This code uses the win32com.client module to create a Photoshop.Application object that allows us to control Adobe Photoshop programmatically. It also uses the tkinter module to create a GUI window with file browse, dimension input, and "Resize" and "Save" buttons. When the user clicks the "Resize" button, the code opens the selected image file in Photoshop, resizes it to the specified dimensions, saves it with a new filename, and displays a success message to the user.
add a function that it will export the output as pngHere's an example code that uses the Python Photoshop API to automate image resizing in Adobe Photoshop with a GUI that allows user input of dimensions, filename, and a save button:
Code: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()
This code uses the win32com.client module to create a Photoshop.Application object that allows us to control Adobe Photoshop programmatically. It also uses the tkinter module to create a GUI window with file browse, dimension input, and "Resize" and "Save" buttons. When the user clicks the "Resize" button, the code opens the selected image file in Photoshop, resizes it to the specified dimensions, saves it with a new filename, and displays a success message to the user.
# Import necessary modules
import win32com.client
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
# Create a function to resize image
def resize_image():
# Get user input for width and height with aspect ratio
width = int(width_entry.get())
height = int(height_entry.get())
aspect_ratio = width / height
# Open the image file
app = win32com.client.Dispatch("Photoshop.Application")
doc = app.Open(file_path)
# Resize the image with aspect ratio
if doc.Width / doc.Height >= aspect_ratio:
doc.ResizeImage(UnitValue(width,"px"), None, None, ResampleMethod.BICUBIC)
else:
doc.ResizeImage(None, UnitValue(height,"px"), None, ResampleMethod.BICUBIC)
# Save the image as a PNG file
save_options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
save_options.Compression = 9
save_options.Interlaced = False
doc.SaveAs(png_path, save_options, True)
# Close the image file
doc.Close(2)
# Create a function to select image file
def select_file():
global file_path
file_path = filedialog.askopenfilename()
# Update label with file name
file_label.config(text=file_path.split("/")[-1])
# Create a function to select output file path
def select_output():
global png_path
png_path = filedialog.asksaveasfilename(defaultextension=".png")
# Update label with file name
output_label.config(text=png_path.split("/")[-1])
# Create a GUI
root = Tk()
root.title("Image Resizer")
# Create label for image file selection
file_label = Label(root, text="Select Image File:")
file_label.grid(row=0, column=0)
# Create button for image file selection
file_button = Button(root, text="Select", command=select_file)
file_button.grid(row=0, column=1)
# Create label for output file selection
output_label = Label(root, text="Select Output File:")
output_label.grid(row=1, column=0)
# Create button for output file selection
output_button = Button(root, text="Select", command=select_output)
output_button.grid(row=1, column=1)
# Create label for width input
width_label = Label(root, text="Width:")
width_label.grid(row=2, column=0)
# Create entry for width input
width_entry = Entry(root)
width_entry.grid(row=2, column=1)
# Create label for height input
height_label = Label(root, text="Height:")
height_label.grid(row=3, column=0)
# Create entry for height input
height_entry = Entry(root)
height_entry.grid(row=3, column=1)
# Create button to resize image
resize_button = Button(root, text="Resize", command=resize_image)
resize_button.grid(row=4, column=0, columnspan=2)
root.mainloop()
win32com
module to communicate with Adobe Photoshop through its automation interface. It also uses the tkinter
module to create a GUI that allows the user input of dimension with aspect ratio and the selection of an image file and output file path. Make a function that will export the photoshop file as png fileHere's a complete code in Python that automates image resizing in Adobe Photoshop using Python Photoshop API with a Graphical User Interface (GUI):
Code:# Import necessary modules import win32com.client from tkinter import * from tkinter import ttk from tkinter import filedialog # Create a function to resize image def resize_image(): # Get user input for width and height with aspect ratio width = int(width_entry.get()) height = int(height_entry.get()) aspect_ratio = width / height # Open the image file app = win32com.client.Dispatch("Photoshop.Application") doc = app.Open(file_path) # Resize the image with aspect ratio if doc.Width / doc.Height >= aspect_ratio: doc.ResizeImage(UnitValue(width,"px"), None, None, ResampleMethod.BICUBIC) else: doc.ResizeImage(None, UnitValue(height,"px"), None, ResampleMethod.BICUBIC) # Save the image as a PNG file save_options = win32com.client.Dispatch('Photoshop.PNGSaveOptions') save_options.Compression = 9 save_options.Interlaced = False doc.SaveAs(png_path, save_options, True) # Close the image file doc.Close(2) # Create a function to select image file def select_file(): global file_path file_path = filedialog.askopenfilename() # Update label with file name file_label.config(text=file_path.split("/")[-1]) # Create a function to select output file path def select_output(): global png_path png_path = filedialog.asksaveasfilename(defaultextension=".png") # Update label with file name output_label.config(text=png_path.split("/")[-1]) # Create a GUI root = Tk() root.title("Image Resizer") # Create label for image file selection file_label = Label(root, text="Select Image File:") file_label.grid(row=0, column=0) # Create button for image file selection file_button = Button(root, text="Select", command=select_file) file_button.grid(row=0, column=1) # Create label for output file selection output_label = Label(root, text="Select Output File:") output_label.grid(row=1, column=0) # Create button for output file selection output_button = Button(root, text="Select", command=select_output) output_button.grid(row=1, column=1) # Create label for width input width_label = Label(root, text="Width:") width_label.grid(row=2, column=0) # Create entry for width input width_entry = Entry(root) width_entry.grid(row=2, column=1) # Create label for height input height_label = Label(root, text="Height:") height_label.grid(row=3, column=0) # Create entry for height input height_entry = Entry(root) height_entry.grid(row=3, column=1) # Create button to resize image resize_button = Button(root, text="Resize", command=resize_image) resize_button.grid(row=4, column=0, columnspan=2) root.mainloop()
This program uses thewin32com
module to communicate with Adobe Photoshop through its automation interface. It also uses thetkinter
module to create a GUI that allows the user input of dimension with aspect ratio and the selection of an image file and output file path.
To use this program, simply run it in your Python environment and follow the on-screen prompts. Once you have made your selections and entered the desired dimensions, click the "Resize" button to resize and export the image file in PNG format.
Sure, here's a function that exports a Photoshop file to PNG format:Make a function that will export the photoshop file as png file
import win32com.client
def export_to_png(file_path, png_path):
# Open the file in Photoshop
app = win32com.client.Dispatch("Photoshop.Application")
doc = app.Open(file_path)
# Create PNG save options
save_options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
save_options.Compression = 9
save_options.Interlaced = False
# Save as PNG and close the file
doc.SaveAs(png_path, save_options, True)
doc.Close(2)
file_path
is the path of the Photoshop file you want to export, and png_path
is the path of the PNG file you want to save. It uses the win32com
module to communicate with Photoshop and open the file, and then creates PNG save options with compression level 9 and interlaced set to False. Finally, it saves the file as a PNG and closes the Photoshop file.# Call resize_image function
resize_image()
# Export resized image to PNG format
export_to_png(file_path, png_path)
change the code that it will resize the image proportional to the aspect ratioCode:# Import necessary modules import win32com.client from tkinter import * from tkinter import ttk from tkinter import filedialog # Create a function to resize image def resize_image(): # Get user input for width and height with aspect ratio width = int(width_entry.get()) height = int(height_entry.get()) aspect_ratio = width / height # Open the image file app = win32com.client.Dispatch("Photoshop.Application") doc = app.Open(file_path) # Resize the image with aspect ratio if doc.Width / doc.Height >= aspect_ratio: doc.ResizeImage(UnitValue(width,"px"), None, None, ResampleMethod.BICUBIC) else: doc.ResizeImage(None, UnitValue(height,"px"), None, ResampleMethod.BICUBIC) # Save the image as a PNG file save_options = win32com.client.Dispatch('Photoshop.PNGSaveOptions') save_options.Compression = 9 save_options.Interlaced = False doc.SaveAs(png_path, save_options, True) # Close the image file doc.Close(2) # Create a function to select image file def select_file(): global file_path file_path = filedialog.askopenfilename() # Update label with file name file_label.config(text=file_path.split("/")[-1]) # Create a function to select output file path def select_output(): global png_path png_path = filedialog.asksaveasfilename(defaultextension=".png") # Update label with file name output_label.config(text=png_path.split("/")[-1]) # Create a GUI root = Tk() root.title("Image Resizer") # Create label for image file selection file_label = Label(root, text="Select Image File:") file_label.grid(row=0, column=0) # Create button for image file selection file_button = Button(root, text="Select", command=select_file) file_button.grid(row=0, column=1) # Create label for output file selection output_label = Label(root, text="Select Output File:") output_label.grid(row=1, column=0) # Create button for output file selection output_button = Button(root, text="Select", command=select_output) output_button.grid(row=1, column=1) # Create label for width input width_label = Label(root, text="Width:") width_label.grid(row=2, column=0) # Create entry for width input width_entry = Entry(root) width_entry.grid(row=2, column=1) # Create label for height input height_label = Label(root, text="Height:") height_label.grid(row=3, column=0) # Create entry for height input height_entry = Entry(root) height_entry.grid(row=3, column=1) # Create button to resize image resize_button = Button(root, text="Resize", command=resize_image) resize_button.grid(row=4, column=0, columnspan=2) root.mainloop()
Sure, here's the updated code that resizes the image proportionally to the aspect ratio:change the code that it will resize the image proportional to the aspect ratio
# Import necessary modules
import win32com.client
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
# Create a function to resize image
def resize_image():
# Get user input for width and height
width = int(width_entry.get())
height = int(height_entry.get())
# Open the image file
app = win32com.client.Dispatch("Photoshop.Application")
doc = app.Open(file_path)
# Get the aspect ratio of the image
aspect_ratio = doc.Width / doc.Height
# Calculate the new dimensions with aspect ratio
if width / height > aspect_ratio:
new_width = int(height * aspect_ratio)
new_height = height
else:
new_width = width
new_height = int(width / aspect_ratio)
# Resize the image with aspect ratio
doc.ResizeImage(UnitValue(new_width,"px"), UnitValue(new_height,"px"), None, ResampleMethod.BICUBIC)
# Save the image as a PNG file
save_options = win32com.client.Dispatch('Photoshop.PNGSaveOptions')
save_options.Compression = 9
save_options.Interlaced = False
doc.SaveAs(png_path, save_options, True)
# Close the image file
doc.Close(2)
# Create a function to select image file
def select_file():
global file_path
file_path = filedialog.askopenfilename()
# Update label with file name
file_label.config(text=file_path.split("/")[-1])
# Create a function to select output file path
def select_output():
global png_path
png_path = filedialog.asksaveasfilename(defaultextension=".png")
# Update label with file name
output_label.config(text=png_path.split("/")[-1])
# Create a GUI
root = Tk()
root.title("Image Resizer")
# Create label for image file selection
file_label = Label(root, text="Select Image File:")
file_label.grid(row=0, column=0)
# Create button for image file selection
file_button = Button(root, text="Select", command=select_file)
file_button.grid(row=0, column=1)
# Create label for output file selection
output_label = Label(root, text="Select Output File:")
output_label.grid(row=1, column=0)
# Create button for output file selection
output_button = Button(root, text="Select", command=select_output)
output_button.grid(row=1, column=1)
# Create label for width input
width_label = Label(root, text="Width:")
width_label.grid(row=2, column=0)
# Create entry for width input
width_entry = Entry(root)
width_entry.grid(row=2, column=1)
# Create label for height input
height_label = Label(root, text="Height:")
height_label.grid(row=3, column=0)
# Create entry for height input
height_entry = Entry(root)
height_entry.grid(row=3, column=1)
# Create button to resize image
resize_button = Button(root, text="Resize", command=resize_image)
resize_button.grid(row=4, column=0, columnspan=2)
root.mainloop()
import os
import tkinter as tk
from tkinter import filedialog
from win32com.client import Dispatch
# Create an instance of Photoshop Application
photoshop = Dispatch("Photoshop.Application")
# Create a GUI window
root = tk.Tk()
root.title("Adobe Photoshop Image Resizer")
# Set the default width to 8 inches
DEFAULT_WIDTH = 8
# Function to resize image
def resize_image(filepath, output_folder, width=None, height=None):
# Open image file in Photoshop
doc = photoshop.Open(filepath)
# Get the current dimensions of the image
current_width = doc.width
current_height = doc.height
# Calculate the new dimensions based on user input
if width and not height:
new_width = width
new_height = int((current_height / current_width) * new_width)
elif height and not width:
new_height = height
new_width = int((current_width / current_height) * new_height)
# Resize the image
doc.ResizeImage(new_width, new_height)
# Save the image as PNG in the specified output folder
output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".png"
output_filepath = os.path.join(output_folder, output_filename)
options = Dispatch("Photoshop.PNGSaveOptions")
doc.SaveAs(output_filepath, options)
# Close the image file
doc.Close()
# Function to handle file selection
def select_files():
# Prompt user to select image files
filetypes = [("JPEG Images", "*.jpg"), ("PNG Images", "*.png")]
files = filedialog.askopenfilenames(filetypes=filetypes)
# Update the listbox with selected files
for f in files:
listbox.insert(tk.END, f)
# Function to handle resize button click
def resize_images():
# Get the width or height input from user
if width_entry.get():
width = int(width_entry.get())
height = None
elif height_entry.get():
height = int(height_entry.get())
width = None
else:
width = DEFAULT_WIDTH
height = None
# Get the output folder from user
output_folder = filedialog.askdirectory()
# Resize each selected image file
for f in listbox.get(0, tk.END):
resize_image(f, output_folder, width, height)
# Display success message
success_label.config(text="Images successfully resized and saved as PNG files.")
# Create GUI elements
file_frame = tk.Frame(root)
file_frame.pack(fill=tk.X)
select_button = tk.Button(file_frame, text="Select Files", command=select_files)
select_button.pack(side=tk.LEFT, padx=5, pady=5)
listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE)
listbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
resize_frame = tk.Frame(root)
resize_frame.pack(fill=tk.X)
width_label = tk.Label(resize_frame, text="Width (inches):")
width_label.pack(side=tk.LEFT, padx=5, pady=5)
width_entry = tk.Entry(resize_frame)
width_entry.pack(side=tk.LEFT, padx=5, pady=5)
height_label = tk.Label(resize_frame, text="Height (inches):")
height_label.pack(side=tk.LEFT, padx=5, pady=5)
height_entry = tk.Entry(resize_frame)
height_entry.pack(side=tk.LEFT, padx=5, pady=5)
resize_button = tk.Button(resize_frame, text="Resize Images", command=resize_images)
resize_button.pack(side=tk.LEFT, padx=5, pady=5)
success_label = tk.Label(root, fg="green")
success_label.pack(fill=tk.X, padx=5, pady=5)
# Run the GUI window
root.mainloop()
.py
extension and run it. A window will appear with a "Select Files" button for you to choose the images you want to resize. You can select multiple files by holding down the Ctrl or Shift key while clicking the files. add a function that can change the ruler units from with options px, inches and mm and add it in GUI as radio buttons as wellSure, here is a complete code written in Python language that automates tasks in Adobe Photoshop with a Graphical User Interface and has the functions you requested.
Code:import os import tkinter as tk from tkinter import filedialog from win32com.client import Dispatch # Create an instance of Photoshop Application photoshop = Dispatch("Photoshop.Application") # Create a GUI window root = tk.Tk() root.title("Adobe Photoshop Image Resizer") # Set the default width to 8 inches DEFAULT_WIDTH = 8 # Function to resize image def resize_image(filepath, output_folder, width=None, height=None): # Open image file in Photoshop doc = photoshop.Open(filepath) # Get the current dimensions of the image current_width = doc.width current_height = doc.height # Calculate the new dimensions based on user input if width and not height: new_width = width new_height = int((current_height / current_width) * new_width) elif height and not width: new_height = height new_width = int((current_width / current_height) * new_height) # Resize the image doc.ResizeImage(new_width, new_height) # Save the image as PNG in the specified output folder output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".png" output_filepath = os.path.join(output_folder, output_filename) options = Dispatch("Photoshop.PNGSaveOptions") doc.SaveAs(output_filepath, options) # Close the image file doc.Close() # Function to handle file selection def select_files(): # Prompt user to select image files filetypes = [("JPEG Images", "*.jpg"), ("PNG Images", "*.png")] files = filedialog.askopenfilenames(filetypes=filetypes) # Update the listbox with selected files for f in files: listbox.insert(tk.END, f) # Function to handle resize button click def resize_images(): # Get the width or height input from user if width_entry.get(): width = int(width_entry.get()) height = None elif height_entry.get(): height = int(height_entry.get()) width = None else: width = DEFAULT_WIDTH height = None # Get the output folder from user output_folder = filedialog.askdirectory() # Resize each selected image file for f in listbox.get(0, tk.END): resize_image(f, output_folder, width, height) # Display success message success_label.config(text="Images successfully resized and saved as PNG files.") # Create GUI elements file_frame = tk.Frame(root) file_frame.pack(fill=tk.X) select_button = tk.Button(file_frame, text="Select Files", command=select_files) select_button.pack(side=tk.LEFT, padx=5, pady=5) listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE) listbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) resize_frame = tk.Frame(root) resize_frame.pack(fill=tk.X) width_label = tk.Label(resize_frame, text="Width (inches):") width_label.pack(side=tk.LEFT, padx=5, pady=5) width_entry = tk.Entry(resize_frame) width_entry.pack(side=tk.LEFT, padx=5, pady=5) height_label = tk.Label(resize_frame, text="Height (inches):") height_label.pack(side=tk.LEFT, padx=5, pady=5) height_entry = tk.Entry(resize_frame) height_entry.pack(side=tk.LEFT, padx=5, pady=5) resize_button = tk.Button(resize_frame, text="Resize Images", command=resize_images) resize_button.pack(side=tk.LEFT, padx=5, pady=5) success_label = tk.Label(root, fg="green") success_label.pack(fill=tk.X, padx=5, pady=5) # Run the GUI window root.mainloop()
To use this code, simply save it as a Python file with a.py
extension and run it. A window will appear with a "Select Files" button for you to choose the images you want to resize. You can select multiple files by holding down the Ctrl or Shift key while clicking the files.
Once you have selected the files, you can set the width or height in inches in the text boxes provided. If you leave both boxes blank, the default width of 8 inches will be used. Click the "Resize Images" button to resize and export the images as PNG files. You will be prompted to select a folder to save the exported files.
After the images have been resized and saved, a success message will be displayed at the bottom of the window.
Sure, here's the modified code with the added feature of changing ruler units.add a function that can change the ruler units from with options px, inches and mm and add it in GUI as radio buttons as well
import os
import tkinter as tk
from tkinter import filedialog
from win32com.client import Dispatch
# Create an instance of Photoshop Application
photoshop = Dispatch("Photoshop.Application")
# Create a GUI window
root = tk.Tk()
root.title("Adobe Photoshop Image Resizer")
# Set the default width to 8 inches
DEFAULT_WIDTH = 8
# Function to resize image
def resize_image(filepath, output_folder, width=None, height=None):
# Open image file in Photoshop
doc = photoshop.Open(filepath)
# Get the current dimensions of the image
current_width = doc.width
current_height = doc.height
# Calculate the new dimensions based on user input
if width and not height:
new_width = width
new_height = int((current_height / current_width) * new_width)
elif height and not width:
new_height = height
new_width = int((current_width / current_height) * new_height)
# Resize the image
doc.ResizeImage(new_width, new_height)
# Save the image as PNG in the specified output folder
output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".png"
output_filepath = os.path.join(output_folder, output_filename)
options = Dispatch("Photoshop.PNGSaveOptions")
doc.SaveAs(output_filepath, options)
# Close the image file
doc.Close()
# Function to handle file selection
def select_files():
# Prompt user to select image files
filetypes = [("JPEG Images", "[I].jpg"), ("PNG Images", "[/I].png")]
files = filedialog.askopenfilenames(filetypes=filetypes)
# Update the listbox with selected files
for f in files:
listbox.insert(tk.END, f)
# Function to handle resize button click
def resize_images():
# Get the width or height input from user
if width_entry.get():
width = int(width_entry.get())
height = None
elif height_entry.get():
height = int(height_entry.get())
width = None
else:
width = DEFAULT_WIDTH
height = None
# Set the ruler units based on user selection
if px_radio.get() == 1:
photoshop.Preferences.RulerUnits = 1
elif inches_radio.get() == 1:
photoshop.Preferences.RulerUnits = 2
elif mm_radio.get() == 1:
photoshop.Preferences.RulerUnits = 5
# Get the output folder from user
output_folder = filedialog.askdirectory()
# Resize each selected image file
for f in listbox.get(0, tk.END):
resize_image(f, output_folder, width, height)
# Display success message
success_label.config(text="Images successfully resized and saved as PNG files.")
# Create GUI elements
file_frame = tk.Frame(root)
file_frame.pack(fill=tk.X)
select_button = tk.Button(file_frame, text="Select Files", command=select_files)
select_button.pack(side=tk.LEFT, padx=5, pady=5)
listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE)
listbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
resize_frame = tk.Frame(root)
resize_frame.pack(fill=tk.X)
width_label = tk.Label(resize_frame, text="Width (inches):")
width_label.pack(side=tk.LEFT, padx=5, pady=5)
width_entry = tk.Entry(resize_frame)
width_entry.pack(side=tk.LEFT, padx=5, pady=5)
height_label = tk.Label(resize_frame, text="Height (inches):")
height_label.pack(side=tk.LEFT, padx=5, pady=5)
height_entry = tk.Entry(resize_frame)
height_entry.pack(side=tk.LEFT, padx=5, pady=5)
ruler_frame = tk.Frame(root)
ruler_frame.pack(fill=tk.X)
px_radio = tk.IntVar(value=1)
inches_radio = tk.IntVar()
mm_radio = tk.IntVar()
px_radio_button = tk.Radiobutton(ruler_frame, text="Pixels", variable
your post got cut, can you give the code give the code again but this time, start from this lineSure, here's the modified code with the added feature of changing ruler units.
Sure, here's the modified code with the added feature of changing ruler units.your post got cut, can you give the code give the code again but this time, start from this line
mm_radio = tk.IntVar()
import os
import tkinter as tk
from tkinter import filedialog
from win32com.client import Dispatch
# Create an instance of Photoshop Application
photoshop = Dispatch("Photoshop.Application")
# Create a GUI window
root = tk.Tk()
root.title("Adobe Photoshop Image Resizer")
# Set the default width to 8 inches
DEFAULT_WIDTH = 8
# Function to resize image
def resize_image(filepath, output_folder, width=None, height=None):
# Open image file in Photoshop
doc = photoshop.Open(filepath)
# Get the current dimensions of the image
current_width = doc.width
current_height = doc.height
# Calculate the new dimensions based on user input
if width and not height:
new_width = width
new_height = int((current_height / current_width) * new_width)
elif height and not width:
new_height = height
new_width = int((current_width / current_height) * new_height)
# Resize the image
doc.ResizeImage(new_width, new_height)
# Save the image as PNG in the specified output folder
output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".png"
output_filepath = os.path.join(output_folder, output_filename)
options = Dispatch("Photoshop.PNGSaveOptions")
doc.SaveAs(output_filepath, options)
# Close the image file
doc.Close()
# Function to handle file selection
def select_files():
# Prompt user to select image files
filetypes = [("JPEG Images", "[I].jpg"), ("PNG Images", "[/I].png")]
files = filedialog.askopenfilenames(filetypes=filetypes)
# Update the listbox with selected files
for f in files:
listbox.insert(tk.END, f)
# Function to handle resize button click
def resize_images():
# Get the width or height input from user
if width_entry.get():
width = int(width_entry.get())
height = None
elif height_entry.get():
height = int(height_entry.get())
width = None
else:
width = DEFAULT_WIDTH
height = None
# Set the ruler units based on user selection
if px_radio.get() == 1:
photoshop.Preferences.RulerUnits = 1
elif inches_radio.get() == 1:
photoshop.Preferences.RulerUnits = 2
elif mm_radio.get() == 1:
photoshop.Preferences.RulerUnits = 5
# Get the output folder from user
output_folder = filedialog.askdirectory()
# Resize each selected image file
for f in listbox.get(0, tk.END):
resize_image(f, output_folder, width, height)
# Display success message
success_label.config(text="Images successfully resized and saved as PNG files.")
# Create GUI elements
file_frame = tk.Frame(root)
file_frame.pack(fill=tk.X)
select_button = tk.Button(file_frame, text="Select Files", command=select_files)
select_button.pack(side=tk.LEFT, padx=5, pady=5)
listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE)
listbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)
resize_frame = tk.Frame(root)
resize_frame.pack(fill=tk.X)
width_label = tk.Label(resize_frame, text="Width (inches):")
width_label.pack(side=tk.LEFT, padx=5, pady=5)
width_entry = tk.Entry(resize_frame)
width_entry.pack(side=tk.LEFT, padx=5, pady=5)
height_label = tk.Label(resize_frame, text="Height (inches):")
height_label.pack(side=tk.LEFT, padx=5, pady=5)
height_entry = tk.Entry(resize_frame)
height_entry.pack(side=tk.LEFT, padx=5, pady=5)
ruler_frame = tk.Frame(root)
ruler_frame.pack(fill=tk.X)
px_radio = tk.IntVar(value=1)
inches_radio = tk.IntVar()
mm_radio = tk.IntVar()
px_radio_button = tk.Radiobutton(ruler_frame, text="Pixels", variable=px_radio, value=1)
px_radio_button.pack(side=tk.LEFT, padx=5, pady=5)
inches
can you write this code in a text file so i can get the whole script?Sure, here's the modified code with the added feature of changing ruler units.
Code:import os import tkinter as tk from tkinter import filedialog from win32com.client import Dispatch # Create an instance of Photoshop Application photoshop = Dispatch("Photoshop.Application") # Create a GUI window root = tk.Tk() root.title("Adobe Photoshop Image Resizer") # Set the default width to 8 inches DEFAULT_WIDTH = 8 # Function to resize image def resize_image(filepath, output_folder, width=None, height=None): # Open image file in Photoshop doc = photoshop.Open(filepath) # Get the current dimensions of the image current_width = doc.width current_height = doc.height # Calculate the new dimensions based on user input if width and not height: new_width = width new_height = int((current_height / current_width) * new_width) elif height and not width: new_height = height new_width = int((current_width / current_height) * new_height) # Resize the image doc.ResizeImage(new_width, new_height) # Save the image as PNG in the specified output folder output_filename = os.path.splitext(os.path.basename(filepath))[0] + ".png" output_filepath = os.path.join(output_folder, output_filename) options = Dispatch("Photoshop.PNGSaveOptions") doc.SaveAs(output_filepath, options) # Close the image file doc.Close() # Function to handle file selection def select_files(): # Prompt user to select image files filetypes = [("JPEG Images", "[I].jpg"), ("PNG Images", "[/I].png")] files = filedialog.askopenfilenames(filetypes=filetypes) # Update the listbox with selected files for f in files: listbox.insert(tk.END, f) # Function to handle resize button click def resize_images(): # Get the width or height input from user if width_entry.get(): width = int(width_entry.get()) height = None elif height_entry.get(): height = int(height_entry.get()) width = None else: width = DEFAULT_WIDTH height = None # Set the ruler units based on user selection if px_radio.get() == 1: photoshop.Preferences.RulerUnits = 1 elif inches_radio.get() == 1: photoshop.Preferences.RulerUnits = 2 elif mm_radio.get() == 1: photoshop.Preferences.RulerUnits = 5 # Get the output folder from user output_folder = filedialog.askdirectory() # Resize each selected image file for f in listbox.get(0, tk.END): resize_image(f, output_folder, width, height) # Display success message success_label.config(text="Images successfully resized and saved as PNG files.") # Create GUI elements file_frame = tk.Frame(root) file_frame.pack(fill=tk.X) select_button = tk.Button(file_frame, text="Select Files", command=select_files) select_button.pack(side=tk.LEFT, padx=5, pady=5) listbox = tk.Listbox(file_frame, selectmode=tk.MULTIPLE) listbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5) resize_frame = tk.Frame(root) resize_frame.pack(fill=tk.X) width_label = tk.Label(resize_frame, text="Width (inches):") width_label.pack(side=tk.LEFT, padx=5, pady=5) width_entry = tk.Entry(resize_frame) width_entry.pack(side=tk.LEFT, padx=5, pady=5) height_label = tk.Label(resize_frame, text="Height (inches):") height_label.pack(side=tk.LEFT, padx=5, pady=5) height_entry = tk.Entry(resize_frame) height_entry.pack(side=tk.LEFT, padx=5, pady=5) ruler_frame = tk.Frame(root) ruler_frame.pack(fill=tk.X) px_radio = tk.IntVar(value=1) inches_radio = tk.IntVar() mm_radio = tk.IntVar() px_radio_button = tk.Radiobutton(ruler_frame, text="Pixels", variable=px_radio, value=1) px_radio_button.pack(side=tk.LEFT, padx=5, pady=5) inches