-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPhotoTool_v1.1.py
145 lines (116 loc) · 6.12 KB
/
PhotoTool_v1.1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
from tkinter import Tk, Button, messagebox, ttk, simpledialog, Toplevel, IntVar
from PIL import Image
import piexif
def strip_metadata():
directory = "_pics_"
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'rb') as f:
image = Image.open(f)
image_data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(image_data)
image_without_exif.save(filepath, "JPEG")
messagebox.showinfo("Success", "Metadata stripped from all photos!")
def resize_images():
directory = "_pics_"
percentage = simpledialog.askfloat("Input", "Enter resize percentage (e.g., 50 for 50%):", parent=root)
if percentage:
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'rb') as f:
image = Image.open(f)
new_size = (int(image.width * (percentage / 100)), int(image.height * (percentage / 100)))
resized_image = image.resize(new_size)
resized_image.save(filepath, "JPEG")
messagebox.showinfo("Success", f"Resized all photos to {percentage}% of their original size.")
def compress_images():
directory = "_pics_"
compression_quality = simpledialog.askinteger("Input", "Enter compression number (100 is no compression):", parent=root, minvalue=1, maxvalue=100)
if compression_quality:
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'rb') as f:
image = Image.open(f)
image.save(filepath, "JPEG", quality=compression_quality)
messagebox.showinfo("Success", f"Compressed all photos with quality {compression_quality}.")
def add_keywords_to_images():
directory = "_pics_"
keywords_input = simpledialog.askstring("Input", "Enter keywords separated by commas:", parent=root)
if keywords_input:
keywords = keywords_input.encode("utf-16")
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'rb') as f:
exif_dict = piexif.load(f.read())
exif_dict["0th"][piexif.ImageIFD.XPKeywords] = keywords
exif_bytes = piexif.dump(exif_dict)
with Image.open(filepath) as img:
img.save(filepath, "JPEG", exif=exif_bytes)
messagebox.showinfo("Success", "Keywords added to all photos!")
def add_specific_metadata():
directory = "_pics_"
artist = simpledialog.askstring("Metadata Input", "Enter Artist Name:", parent=root)
copyright = simpledialog.askstring("Metadata Input", "Enter Copyright Info:", parent=root)
for filename in os.listdir(directory):
if filename.endswith(".jpg") or filename.endswith(".jpeg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'rb') as f:
exif_dict = piexif.load(f.read())
if artist:
exif_dict["0th"][piexif.ImageIFD.Artist] = artist
if copyright:
exif_dict["0th"][piexif.ImageIFD.Copyright] = copyright
exif_bytes = piexif.dump(exif_dict)
with Image.open(filepath) as img:
img.save(filepath, "JPEG", exif=exif_bytes)
messagebox.showinfo("Success", "Metadata added to all photos!")
def convert_images():
directory = "_pics_"
format_options = [".jpg", ".png", ".webp"]
def submit_conversion():
choice = var.get()
if choice < len(format_options):
selected_format = format_options[choice]
for filename in os.listdir(directory):
if filename.endswith(tuple(format_options)) and not filename.endswith(selected_format):
filepath = os.path.join(directory, filename)
output_filepath = os.path.splitext(filepath)[0] + selected_format
with Image.open(filepath) as img:
img.save(output_filepath)
window.destroy()
messagebox.showinfo("Success", f"Images converted to {selected_format} format!")
else:
messagebox.showwarning("Error", "Invalid format selected!")
window = Toplevel(root)
window.title("Choose Conversion Format")
var = IntVar()
for i, option in enumerate(format_options):
ttk.Radiobutton(window, text=option, variable=var, value=i).pack(anchor="w", padx=20, pady=5)
ttk.Button(window, text="Convert", command=submit_conversion).pack(pady=10)
root = Tk()
root.title("Photo Processor")
root.geometry("180x460")
root.resizable(0, 0)
root.configure(bg="#2e2e2e")
style = ttk.Style(root)
style.theme_use("clam")
style.configure("TButton", foreground="white", background="#555555", padding=10)
strip_button = ttk.Button(root, text="Strip Metadata", command=strip_metadata)
strip_button.pack(pady=60)
resize_button = ttk.Button(root, text="Resize Images", command=resize_images)
resize_button.pack(pady=5)
compress_button = ttk.Button(root, text="Compress Images", command=compress_images)
compress_button.pack(pady=5)
add_keywords_button = ttk.Button(root, text="Add Keywords", command=add_keywords_to_images)
add_keywords_button.pack(pady=5)
metadata_button = ttk.Button(root, text="Add Metadata", command=add_specific_metadata)
metadata_button.pack(pady=5)
convert_button = ttk.Button(root, text="Convert Images", command=convert_images)
convert_button.pack(pady=5)
root.mainloop()