File size: 11,906 Bytes
a09fcbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b7b4d
a09fcbd
 
 
93b7b4d
a09fcbd
 
 
93b7b4d
a09fcbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b7b4d
 
 
 
 
 
 
 
 
a09fcbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import gradio as gr
from PIL import Image, ExifTags
import pandas as pd
import os
import piexif
import numpy as np
import io
import logging
import shutil
import datetime
from PIL import PngImagePlugin

logging.basicConfig(filename='metadata_update.log', level=logging.INFO, 
                    format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

SUPPORTED_FORMATS = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp')  # Add formats as needed

def extract_metadata(uploaded_files, progress=gr.Progress()):
    outputs = []
    num_files = min(len(uploaded_files), 100)  # Limit to 100 images
    for idx in range(100):
        if idx < num_files:
            image_path = uploaded_files[idx]
            filename = os.path.basename(image_path)
            
            # Check if the file is in a supported format
            if not image_path.lower().endswith(SUPPORTED_FORMATS):
                outputs.extend([None, image_path, filename, "Unsupported format", "", ""])
                continue  # Skip unsupported formats
            
            try:
                img = Image.open(image_path)
                img.thumbnail((600, 600))  # Adjusted thumbnail size
                
                if img.format == 'PNG':
                    title = img.info.get('Title', '')
                    description = img.info.get('Description', '')
                    keywords = img.info.get('Keywords', '')
                else:
                    exif_data = img.getexif() if img else {}
                    exif = {}
                    for tag_id in exif_data:
                        tag = ExifTags.TAGS.get(tag_id, tag_id)
                        data_value = exif_data.get(tag_id)
                        if isinstance(data_value, bytes):
                            try:
                                data_value = data_value.decode('utf-16-le').strip('\x00')
                            except UnicodeDecodeError:
                                data_value = data_value.decode(errors='replace')
                        exif[tag] = data_value

                    title = exif.get('XPTitle', '')
                    description = exif.get('ImageDescription', '')
                    keywords = exif.get('XPKeywords', '')

                # Decode if still bytes
                if isinstance(title, bytes):
                    title = title.decode('utf-16-le').strip('\x00')
                if isinstance(description, bytes):
                    description = description.decode('utf-8')
                if isinstance(keywords, bytes):
                    keywords = keywords.decode('utf-16-le').strip('\x00')

            except Exception as e:
                logger.error(f"Error processing {image_path}: {str(e)}")
                img, title, description, keywords = None, '', '', ''

            outputs.extend([img, image_path, filename, title, description, keywords])
        else:
            outputs.extend([None, '', '', '', '', ''])
        progress((idx + 1) / 100, desc=f"Processing image {idx + 1} of {num_files}")
    return outputs

def write_metadata_to_image(image_path, title, description, keywords):
    try:
        logger.info(f"Attempting to write metadata to {image_path}")
        
        # Open the image file
        with Image.open(image_path) as img:
            # Get the original format
            original_format = img.format
            
            # Prepare EXIF data for JPEG
            if original_format in ['JPEG', 'JPG']:
                exif_dict = piexif.load(img.info.get("exif", b""))
                
                # Update EXIF data
                exif_dict['0th'][piexif.ImageIFD.XPTitle] = title.encode('utf-16le')
                exif_dict['0th'][piexif.ImageIFD.ImageDescription] = description.encode('utf-8')
                exif_dict['0th'][piexif.ImageIFD.XPKeywords] = keywords.encode('utf-16le')
                
                # Convert EXIF data to bytes
                exif_bytes = piexif.dump(exif_dict)
                
                # Save with EXIF data
                img.save(image_path, exif=exif_bytes)
                logger.info(f"Metadata successfully written to {image_path}")
                
            # Prepare metadata for PNG
            elif original_format == 'PNG':
                metadata = PngImagePlugin.PngInfo()
                metadata.add_text("Title", title)
                metadata.add_text("Description", description)
                metadata.add_text("Keywords", keywords)
                
                # Save the image with metadata
                img.save(image_path, format='PNG', pnginfo=metadata)
                logger.info(f"Metadata successfully written to {image_path}")
                
            else:
                logger.error(f"Unsupported format: {original_format}")
                return False
        
        return True
    except Exception as e:
        logger.error(f"Error writing metadata to {image_path}: {str(e)}")
        return False

def update_image_metadata(*args):
    success_count = 0
    error_count = 0
    image_paths = []
    
    # First, extract all image paths
    for i in range(100):
        idx = i * 6
        image_path = args[idx + 1]
        if image_path:
            image_paths.append(image_path)
    
    # Copy images to project directory
    copied_paths = copy_images_to_project_dir(image_paths)
    
    # Now update metadata for copied images
    for i, copied_path in enumerate(copied_paths):
        if copied_path:
            idx = i * 6
            title = args[idx + 3]
            description = args[idx + 4]
            keywords = args[idx + 5]
            if write_metadata_to_image(copied_path, title, description, keywords):
                success_count += 1
            else:
                error_count += 1

    return f"Images copied and metadata updated. Successful updates: {success_count}, Failed updates: {error_count}. Check the 'edited_images' folder for results."

def copy_images_to_project_dir(image_paths):
    copied_paths = []
    for path in image_paths:
        if path and os.path.isfile(path):
            filename = os.path.basename(path)
            new_path = os.path.join("edited_images", filename)
            os.makedirs("edited_images", exist_ok=True)
            shutil.copy2(path, new_path)
            copied_paths.append(new_path)
        else:
            copied_paths.append(None)
    return copied_paths

def save_csv(*args):
    data = []
    for i in range(100):
        idx = i * 6
        img = args[idx]  # Not used, but kept for consistency
        image_path = args[idx + 1]
        filename = args[idx + 2]
        title = args[idx + 3]
        description = args[idx + 4]
        keywords = args[idx + 5]
        if filename:
            data.append({
                'Filename': filename,
                'Title': title,
                'Description': description,
                'Keywords': keywords,
            })
    df = pd.DataFrame(data)
    try:
        # Generate timestamped CSV filename
        timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
        csv_filename = f'edited_metadata_{timestamp}.csv'
        df.to_csv(csv_filename, index=False)
        return csv_filename  # Return CSV path for download
    except Exception as e:
        return f"Error saving data: {e}"


# Functions to update character and keyword counts
def update_title_info(text):
    return f"{len(text)} characters"

def update_description_info(text):
    return f"{len(text)} characters"

def update_keywords_info(text):
    keywords = [k.strip() for k in text.split(',') if k.strip()]
    return f"{len(keywords)} keywords"

with gr.Blocks(css="""
    .counter {
        font-size: 12px;
        color: #555;
        margin-top: -8px;
        margin-bottom: 8px;
    }
    .metadata-section {
        border: 1px solid #ccc;
        padding: 10px;
        margin-bottom: 10px;
        border-radius: 5px;
    }
    .thumbnail {
        flex-shrink: 0;
    }
    .metadata-fields {
        flex-grow: 1;
        margin-left: 10px;
    }
    .container {
        max-width: 1200px;
        margin: auto;
    }
""") as demo:
    gr.Markdown("## Image Metadata Editor")
    with gr.Column():
        file_input = gr.File(label="Select Images", file_count="multiple", type="filepath")
        extract_button = gr.Button("Extract Metadata")
    
    # Lists to hold component references
    image_components = []
    filename_components = []
    title_components = []
    title_info_components = []
    description_components = []
    description_info_components = []
    keywords_components = []
    keywords_info_components = []

    for i in range(100):
        with gr.Row():
            # Thumbnail Image
            with gr.Column(scale=1):
                image = gr.Image(label="Thumbnail", interactive=False, width=600, height=600)
                image_components.append(image)
            # Metadata Fields
            with gr.Column(scale=3):
                filename = gr.Textbox(label="Filename", interactive=False, placeholder="Filename")
                filename_components.append(filename)
                
                title = gr.Textbox(label="Title", lines=1, placeholder="Enter title")
                title_components.append(title)
                title_info = gr.Text(label="", value="0 characters", interactive=False, elem_classes="counter")
                title_info_components.append(title_info)
                
                description = gr.Textbox(label="Description", lines=2, placeholder="Enter description")
                description_components.append(description)
                description_info = gr.Text(label="", value="0 characters", interactive=False, elem_classes="counter")
                description_info_components.append(description_info)
                
                keywords = gr.Textbox(label="Keywords", lines=1, placeholder="Enter keywords separated by commas")
                keywords_components.append(keywords)
                keywords_info = gr.Text(label="", value="0 keywords", interactive=False, elem_classes="counter")
                keywords_info_components.append(keywords_info)
                
                # Link change events to update counters
                title.change(fn=update_title_info, inputs=title, outputs=title_info)
                description.change(fn=update_description_info, inputs=description, outputs=description_info)
                keywords.change(fn=update_keywords_info, inputs=keywords, outputs=keywords_info)

    # Save Buttons and Output
    with gr.Row():
        save_metadata_button = gr.Button("Save Metadata to Images")
        save_csv_button = gr.Button("Save CSV")
        save_output = gr.Textbox(label="Save Status", interactive=False)
        csv_output = gr.File(label="Download CSV")  # File download component

# Update click events
    save_csv_button.click(
        fn=save_csv,
        inputs=all_outputs,
        outputs=[csv_output, save_output]  # Return both the file and status
    )


    # Collect all output components for extraction and saving
    all_outputs = []
    for i in range(100):
        all_outputs.extend([
            image_components[i],
            gr.Textbox(visible=False),  # Hidden textbox to store full image path
            filename_components[i],
            title_components[i],
            description_components[i],
            keywords_components[i]
        ])

    # Define the interactions
    extract_button.click(
        fn=extract_metadata,
        inputs=file_input,
        outputs=all_outputs
    )

    save_metadata_button.click(
        fn=update_image_metadata,
        inputs=all_outputs,
        outputs=save_output
    )

    save_csv_button.click(
        fn=save_csv,
        inputs=all_outputs,
        outputs=save_output
    )

demo.launch()