-
Notifications
You must be signed in to change notification settings - Fork 0
/
supercharge-metadata.py
179 lines (156 loc) · 7.55 KB
/
supercharge-metadata.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# Import necessary libraries
import os
import shutil
import sys
import requests
import piexif
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QProgressBar, QLabel, QFileDialog, QTextEdit
from PyQt5.QtCore import Qt
# Your API token for accessing the PhotoTag.ai service
api_token = "" # Replace with your API token
def fetch_existing_metadata(image_path):
# Try to load and read the existing metadata from an image
try:
exif_data = piexif.load(image_path) # Load the EXIF data of the image
existing_title = ""
existing_description = ""
# Check and process the XPTitle from the EXIF data if it exists
if piexif.ImageIFD.XPTitle in exif_data["0th"]:
xp_title_tuple = exif_data["0th"][piexif.ImageIFD.XPTitle]
xp_title_bytes = bytes(xp_title_tuple) # Convert the tuple to bytes
existing_title = xp_title_bytes.decode('utf-16le', errors='ignore').rstrip('\x00') # Decode bytes to string
# Check and process the ImageDescription from the EXIF data if it exists
if piexif.ImageIFD.ImageDescription in exif_data["0th"]:
image_description_bytes = exif_data["0th"][piexif.ImageIFD.ImageDescription]
existing_description = image_description_bytes.decode('utf-8', errors='ignore').strip() # Decode bytes to string
# Print existing metadata for debugging
print(f"Existing metadata read for image: {image_path} - Title: {existing_title}, Description: {existing_description}")
return existing_title, existing_description
except Exception as e:
# If any error occurs, print the error message
print(f"Error reading existing metadata: {str(e)}")
return '', ''
def get_image_metadata(image_path):
# Fetch existing metadata to use as context for the API request
existing_title, existing_description = fetch_existing_metadata(image_path)
custom_context = f"{existing_title} {existing_description}".strip()
# Define the API request details
url = "https://server.phototag.ai/api/keywords"
headers = {"Authorization": f"Bearer {api_token}"}
payload = {
"language": "en",
"maxKeywords": 40,
"customContext": custom_context
}
# Make the API request with the image and context
with open(image_path, 'rb') as img_file:
files = {"file": img_file}
response = requests.post(url, headers=headers, data=payload, files=files)
# Process the API response
if response.status_code == 200:
data = response.json().get("data")
if data:
# Extract and print the fetched metadata
title = data.get("title", "")
description = data.get("description", "")
keywords = data.get("keywords", [])
print(f"Metadata fetched for image: {image_path}")
print(f"Title: {title}")
print(f"Description: {description}")
print(f"Keywords: {keywords}")
return title, description, keywords
else:
# Print error message if the API call fails
print(f"Failed to fetch metadata. Check your API token. Status code: {response.status_code}")
return None, None, []
def write_metadata_to_image(image_path, title, description, keywords):
# Attempt to write the fetched metadata back into the image's EXIF data
try:
exif_dict = piexif.load(image_path) # Load existing EXIF data
# Prepare the metadata for insertion
keywords_str = ', '.join(keywords)
title_bytes = title.encode('utf-16le')
description_bytes = description.encode('utf-8')
keywords_bytes = keywords_str.encode('utf-16le')
# Update the EXIF data with the new metadata
exif_dict['0th'][piexif.ImageIFD.ImageDescription] = description_bytes
exif_dict['0th'][piexif.ImageIFD.XPTitle] = title_bytes
exif_dict['0th'][piexif.ImageIFD.XPKeywords] = keywords_bytes
# Save the updated EXIF data back to the image
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, image_path)
print(f"Metadata successfully written to the image: {image_path}")
return True
except Exception as e:
# Print error message if writing metadata fails
print(f"Failed to write metadata to the image: {str(e)}")
return False
# GUI Application to process a folder of images
class ImageKeywordingTool(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# Set up the application window
self.setWindowTitle('Image Keywording Tool')
self.resize(600, 400)
layout = QVBoxLayout()
# Status message box
self.status_message = QTextEdit()
self.status_message.setPlainText("Processing Not Started")
self.status_message.setReadOnly(True)
# Button to select the folder for processing
self.select_folder_button = QPushButton('Select Folder')
self.select_folder_button.clicked.connect(self.start_processing)
# Progress bar to indicate processing progress
self.progress_bar = QProgressBar()
# Arrange the components in the application window
layout.addWidget(self.status_message)
layout.addWidget(self.select_folder_button)
layout.addWidget(self.progress_bar)
self.setLayout(layout)
def start_processing(self):
# Handler for the 'Select Folder' button click
selected_folder = QFileDialog.getExistingDirectory(self, "Select Folder")
if selected_folder:
self.status_message.setPlainText("Processing Running...")
self.process_images_in_folder(selected_folder)
self.status_message.append("Processing Completed")
self.status_message.append("Close Window to Exit")
def process_images_in_folder(self, folder_path):
# Create 'ready' and 'failed' subfolders for processed images
ready_folder = os.path.join(folder_path, "ready")
failed_folder = os.path.join(folder_path, "failed")
os.makedirs(ready_folder, exist_ok=True)
os.makedirs(failed_folder, exist_ok=True)
# Find all JPEG images in the selected folder
images_to_process = [filename for filename in os.listdir(folder_path) if filename.lower().endswith(('.jpg', '.jpeg'))]
total_images = len(images_to_process)
processed_images = 0
# Process each image in the folder
for filename in images_to_process:
image_path = os.path.join(folder_path, filename)
# Fetch and update metadata for each image
title, description, keywords = get_image_metadata(image_path)
if title and keywords:
success = write_metadata_to_image(image_path, title, description, keywords)
# Move processed images to 'ready' or 'failed' folder
if success:
shutil.move(image_path, os.path.join(ready_folder, filename))
else:
shutil.move(image_path, os.path.join(failed_folder, filename))
else:
shutil.move(image_path, os.path.join(failed_folder, filename))
# Update progress bar
processed_images += 1
progress = processed_images / total_images * 100
self.progress_bar.setValue(int(round(progress)))
QApplication.processEvents() # Keep the GUI responsive
# Main function to run the application
def main():
app = QApplication(sys.argv)
ex = ImageKeywordingTool()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()