Update main.py
Browse files
main.py
CHANGED
|
@@ -1,657 +1,654 @@
|
|
| 1 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
-
from fastapi.responses import HTMLResponse
|
| 3 |
-
from PIL import Image
|
| 4 |
-
from PIL.ExifTags import TAGS
|
| 5 |
-
from datetime import datetime
|
| 6 |
-
import os
|
| 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 |
-
.result-
|
| 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 |
-
.metadata-card.
|
| 255 |
-
border-left-color: var(--
|
| 256 |
-
}
|
| 257 |
-
|
| 258 |
-
.metadata-card
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
}
|
| 267 |
-
|
| 268 |
-
.metadata-card.
|
| 269 |
-
color: var(--
|
| 270 |
-
}
|
| 271 |
-
|
| 272 |
-
.metadata-
|
| 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 |
-
border-bottom:
|
| 304 |
-
}
|
| 305 |
-
|
| 306 |
-
.exif-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
color:
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
color: var(--danger-color);
|
| 326 |
-
}
|
| 327 |
-
|
| 328 |
-
.
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
}
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
}
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
<
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
<
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
</
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
<
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
</
|
| 400 |
-
</
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
const
|
| 409 |
-
const
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
uploadArea.classList.
|
| 426 |
-
}
|
| 427 |
-
|
| 428 |
-
function
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
//
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
<
|
| 494 |
-
<div class="metadata-item">
|
| 495 |
-
<strong>
|
| 496 |
-
<span>${data.
|
| 497 |
-
</div>
|
| 498 |
-
<div class="metadata-item">
|
| 499 |
-
<strong>
|
| 500 |
-
<span>${data.
|
| 501 |
-
</div>
|
| 502 |
-
<div class="metadata-item">
|
| 503 |
-
<strong>
|
| 504 |
-
<span>${data.
|
| 505 |
-
</div>
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
<div class="metadata-item">
|
| 523 |
-
<strong>
|
| 524 |
-
<span>${
|
| 525 |
-
</div>
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
<div class="metadata-item">
|
| 535 |
-
<strong>
|
| 536 |
-
<span>
|
| 537 |
-
</div>
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
<
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
|
| 562 |
-
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
| 581 |
-
|
| 582 |
-
|
| 583 |
-
|
| 584 |
-
|
| 585 |
-
|
| 586 |
-
<
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
|
| 593 |
-
|
| 594 |
-
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
<
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
| 615 |
-
//
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
"""
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
if __name__ == "__main__":
|
| 656 |
-
import uvicorn
|
| 657 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from PIL.ExifTags import TAGS
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
import os
|
| 7 |
+
import tempfile
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="Image Metadata Extractor", description="Upload an image to extract its metadata including creation timestamp")
|
| 10 |
+
|
| 11 |
+
def get_image_metadata(image_path):
|
| 12 |
+
"""Extract metadata from an image file"""
|
| 13 |
+
result = {
|
| 14 |
+
"filename": os.path.basename(image_path),
|
| 15 |
+
"format": None,
|
| 16 |
+
"size": None,
|
| 17 |
+
"mode": None,
|
| 18 |
+
"exif_data": {},
|
| 19 |
+
"creation_date": None,
|
| 20 |
+
"file_system_creation_time": None,
|
| 21 |
+
"error": None
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
# Open the image
|
| 26 |
+
with Image.open(image_path) as img:
|
| 27 |
+
result["format"] = img.format
|
| 28 |
+
result["size"] = img.size
|
| 29 |
+
result["mode"] = img.mode
|
| 30 |
+
|
| 31 |
+
# Get EXIF data
|
| 32 |
+
exif_data = img._getexif()
|
| 33 |
+
if exif_data is not None:
|
| 34 |
+
exif_dict = {}
|
| 35 |
+
for tag_id, value in exif_data.items():
|
| 36 |
+
tag = TAGS.get(tag_id, tag_id)
|
| 37 |
+
exif_dict[tag] = str(value)
|
| 38 |
+
result["exif_data"] = exif_dict
|
| 39 |
+
|
| 40 |
+
# Try to extract creation date from common EXIF tags
|
| 41 |
+
date_tags = ['DateTime', 'DateTimeOriginal', 'DateTimeDigitized']
|
| 42 |
+
for tag in date_tags:
|
| 43 |
+
# Find the tag ID for the current tag
|
| 44 |
+
tag_id = None
|
| 45 |
+
for tid, tname in TAGS.items():
|
| 46 |
+
if tname == tag:
|
| 47 |
+
tag_id = tid
|
| 48 |
+
break
|
| 49 |
+
|
| 50 |
+
if tag_id and tag_id in exif_data:
|
| 51 |
+
raw_date = exif_data[tag_id]
|
| 52 |
+
try:
|
| 53 |
+
# Handle different date formats
|
| 54 |
+
if isinstance(raw_date, bytes):
|
| 55 |
+
raw_date = raw_date.decode('utf-8')
|
| 56 |
+
|
| 57 |
+
# Try different date formats
|
| 58 |
+
formats_to_try = [
|
| 59 |
+
'%Y:%m:%d %H:%M:%S',
|
| 60 |
+
'%Y-%m-%d %H:%M:%S',
|
| 61 |
+
'%Y/%m/%d %H:%M:%S'
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
creation_date = None
|
| 65 |
+
for fmt in formats_to_try:
|
| 66 |
+
try:
|
| 67 |
+
creation_date = datetime.strptime(str(raw_date), fmt)
|
| 68 |
+
break
|
| 69 |
+
except ValueError:
|
| 70 |
+
continue
|
| 71 |
+
|
| 72 |
+
if creation_date:
|
| 73 |
+
result["creation_date"] = {
|
| 74 |
+
"source": tag,
|
| 75 |
+
"date": creation_date.isoformat()
|
| 76 |
+
}
|
| 77 |
+
break
|
| 78 |
+
except Exception:
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
# Fallback: Use file system creation time
|
| 82 |
+
stat = os.stat(image_path)
|
| 83 |
+
creation_time = stat.st_ctime
|
| 84 |
+
result["file_system_creation_time"] = datetime.fromtimestamp(creation_time).isoformat()
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
result["error"] = str(e)
|
| 88 |
+
|
| 89 |
+
return result
|
| 90 |
+
|
| 91 |
+
@app.get("/", response_class=HTMLResponse)
|
| 92 |
+
async def read_root():
|
| 93 |
+
"""Serve the HTML frontend"""
|
| 94 |
+
return """
|
| 95 |
+
<!DOCTYPE html>
|
| 96 |
+
<html>
|
| 97 |
+
<head>
|
| 98 |
+
<title>Image Metadata Extractor</title>
|
| 99 |
+
<style>
|
| 100 |
+
:root {
|
| 101 |
+
--primary-color: #4361ee;
|
| 102 |
+
--secondary-color: #3f37c9;
|
| 103 |
+
--accent-color: #4895ef;
|
| 104 |
+
--success-color: #4cc9f0;
|
| 105 |
+
--light-color: #f8f9fa;
|
| 106 |
+
--dark-color: #212529;
|
| 107 |
+
--danger-color: #f72585;
|
| 108 |
+
--warning-color: #f8961e;
|
| 109 |
+
--info-color: #56cfe1;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
* {
|
| 113 |
+
margin: 0;
|
| 114 |
+
padding: 0;
|
| 115 |
+
box-sizing: border-box;
|
| 116 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
body {
|
| 120 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 121 |
+
min-height: 100vh;
|
| 122 |
+
padding: 20px;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.container {
|
| 126 |
+
max-width: 1000px;
|
| 127 |
+
margin: 0 auto;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
header {
|
| 131 |
+
text-align: center;
|
| 132 |
+
padding: 30px 0;
|
| 133 |
+
color: white;
|
| 134 |
+
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
h1 {
|
| 138 |
+
font-size: 2.5rem;
|
| 139 |
+
margin-bottom: 10px;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.subtitle {
|
| 143 |
+
font-size: 1.2rem;
|
| 144 |
+
opacity: 0.9;
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
.card {
|
| 148 |
+
background: rgba(255, 255, 255, 0.95);
|
| 149 |
+
border-radius: 15px;
|
| 150 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
| 151 |
+
padding: 30px;
|
| 152 |
+
margin-bottom: 30px;
|
| 153 |
+
backdrop-filter: blur(10px);
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
.upload-area {
|
| 157 |
+
border: 3px dashed var(--accent-color);
|
| 158 |
+
border-radius: 12px;
|
| 159 |
+
padding: 40px 20px;
|
| 160 |
+
text-align: center;
|
| 161 |
+
transition: all 0.3s ease;
|
| 162 |
+
background: rgba(72, 149, 239, 0.05);
|
| 163 |
+
cursor: pointer;
|
| 164 |
+
}
|
| 165 |
+
|
| 166 |
+
.upload-area:hover {
|
| 167 |
+
border-color: var(--primary-color);
|
| 168 |
+
background: rgba(67, 97, 238, 0.1);
|
| 169 |
+
transform: translateY(-2px);
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
.upload-area.active {
|
| 173 |
+
border-color: var(--success-color);
|
| 174 |
+
background: rgba(76, 201, 240, 0.1);
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
.upload-icon {
|
| 178 |
+
font-size: 4rem;
|
| 179 |
+
color: var(--accent-color);
|
| 180 |
+
margin-bottom: 20px;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
.upload-text {
|
| 184 |
+
font-size: 1.3rem;
|
| 185 |
+
color: var(--dark-color);
|
| 186 |
+
margin-bottom: 15px;
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
.upload-hint {
|
| 190 |
+
color: #6c757d;
|
| 191 |
+
margin-bottom: 20px;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.file-input {
|
| 195 |
+
display: none;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.upload-btn {
|
| 199 |
+
background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
|
| 200 |
+
color: white;
|
| 201 |
+
border: none;
|
| 202 |
+
padding: 15px 40px;
|
| 203 |
+
font-size: 1.1rem;
|
| 204 |
+
border-radius: 50px;
|
| 205 |
+
cursor: pointer;
|
| 206 |
+
transition: all 0.3s ease;
|
| 207 |
+
box-shadow: 0 4px 15px rgba(67, 97, 238, 0.3);
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
.upload-btn:hover {
|
| 211 |
+
transform: translateY(-2px);
|
| 212 |
+
box-shadow: 0 6px 20px rgba(67, 97, 238, 0.4);
|
| 213 |
+
}
|
| 214 |
+
|
| 215 |
+
.upload-btn:active {
|
| 216 |
+
transform: translateY(0);
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
.result-container {
|
| 220 |
+
display: none;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
.result-header {
|
| 224 |
+
text-align: center;
|
| 225 |
+
margin-bottom: 25px;
|
| 226 |
+
color: var(--dark-color);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.metadata-grid {
|
| 230 |
+
display: grid;
|
| 231 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 232 |
+
gap: 20px;
|
| 233 |
+
margin-bottom: 30px;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
.metadata-card {
|
| 237 |
+
background: white;
|
| 238 |
+
border-radius: 10px;
|
| 239 |
+
padding: 20px;
|
| 240 |
+
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
| 241 |
+
border-left: 4px solid var(--primary-color);
|
| 242 |
+
transition: transform 0.3s ease;
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
.metadata-card:hover {
|
| 246 |
+
transform: translateY(-5px);
|
| 247 |
+
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
|
| 248 |
+
}
|
| 249 |
+
|
| 250 |
+
.metadata-card.creation {
|
| 251 |
+
border-left-color: var(--success-color);
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
.metadata-card.exif {
|
| 255 |
+
border-left-color: var(--warning-color);
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
.metadata-card h3 {
|
| 259 |
+
color: var(--primary-color);
|
| 260 |
+
margin-bottom: 15px;
|
| 261 |
+
font-size: 1.3rem;
|
| 262 |
+
}
|
| 263 |
+
|
| 264 |
+
.metadata-card.creation h3 {
|
| 265 |
+
color: var(--success-color);
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
.metadata-card.exif h3 {
|
| 269 |
+
color: var(--warning-color);
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
.metadata-item {
|
| 273 |
+
margin: 12px 0;
|
| 274 |
+
padding: 10px;
|
| 275 |
+
background: #f8f9fa;
|
| 276 |
+
border-radius: 8px;
|
| 277 |
+
border-left: 3px solid var(--accent-color);
|
| 278 |
+
}
|
| 279 |
+
|
| 280 |
+
.metadata-item strong {
|
| 281 |
+
display: block;
|
| 282 |
+
color: var(--dark-color);
|
| 283 |
+
margin-bottom: 5px;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
.metadata-item span {
|
| 287 |
+
color: #6c757d;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
.exif-list {
|
| 291 |
+
max-height: 300px;
|
| 292 |
+
overflow-y: auto;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
.exif-item {
|
| 296 |
+
display: flex;
|
| 297 |
+
justify-content: space-between;
|
| 298 |
+
padding: 8px 0;
|
| 299 |
+
border-bottom: 1px solid #eee;
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
.exif-item:last-child {
|
| 303 |
+
border-bottom: none;
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
.exif-key {
|
| 307 |
+
font-weight: 500;
|
| 308 |
+
color: var(--dark-color);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
.exif-value {
|
| 312 |
+
color: #6c757d;
|
| 313 |
+
text-align: right;
|
| 314 |
+
max-width: 60%;
|
| 315 |
+
word-break: break-word;
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
.error {
|
| 319 |
+
background: #ffebee;
|
| 320 |
+
border-left-color: var(--danger-color);
|
| 321 |
+
color: var(--danger-color);
|
| 322 |
+
}
|
| 323 |
+
|
| 324 |
+
.error strong {
|
| 325 |
+
color: var(--danger-color);
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.actions {
|
| 329 |
+
text-align: center;
|
| 330 |
+
margin-top: 20px;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
.reset-btn {
|
| 334 |
+
background: transparent;
|
| 335 |
+
color: var(--primary-color);
|
| 336 |
+
border: 2px solid var(--primary-color);
|
| 337 |
+
padding: 12px 30px;
|
| 338 |
+
font-size: 1rem;
|
| 339 |
+
border-radius: 50px;
|
| 340 |
+
cursor: pointer;
|
| 341 |
+
transition: all 0.3s ease;
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
.reset-btn:hover {
|
| 345 |
+
background: var(--primary-color);
|
| 346 |
+
color: white;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
footer {
|
| 350 |
+
text-align: center;
|
| 351 |
+
color: rgba(255, 255, 255, 0.8);
|
| 352 |
+
padding: 20px 0;
|
| 353 |
+
font-size: 0.9rem;
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
@media (max-width: 768px) {
|
| 357 |
+
.metadata-grid {
|
| 358 |
+
grid-template-columns: 1fr;
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
h1 {
|
| 362 |
+
font-size: 2rem;
|
| 363 |
+
}
|
| 364 |
+
|
| 365 |
+
.card {
|
| 366 |
+
padding: 20px;
|
| 367 |
+
}
|
| 368 |
+
}
|
| 369 |
+
</style>
|
| 370 |
+
</head>
|
| 371 |
+
<body>
|
| 372 |
+
<div class="container">
|
| 373 |
+
<header>
|
| 374 |
+
<h1>📸 Image Metadata Extractor</h1>
|
| 375 |
+
<p class="subtitle">Upload any image to extract detailed metadata including creation timestamps</p>
|
| 376 |
+
</header>
|
| 377 |
+
|
| 378 |
+
<main>
|
| 379 |
+
<div class="card">
|
| 380 |
+
<div class="upload-area" id="upload-area">
|
| 381 |
+
<div class="upload-icon">📁</div>
|
| 382 |
+
<h2 class="upload-text">Drag & Drop your image here</h2>
|
| 383 |
+
<p class="upload-hint">or click to browse files</p>
|
| 384 |
+
<input type="file" id="file-input" class="file-input" accept="image/*">
|
| 385 |
+
<button class="upload-btn" id="upload-btn">Select Image</button>
|
| 386 |
+
</div>
|
| 387 |
+
</div>
|
| 388 |
+
|
| 389 |
+
<div class="card result-container" id="result-container">
|
| 390 |
+
<h2 class="result-header">Image Metadata Analysis</h2>
|
| 391 |
+
<div class="metadata-grid" id="metadata-content"></div>
|
| 392 |
+
<div class="actions">
|
| 393 |
+
<button class="reset-btn" id="reset-btn">Analyze Another Image</button>
|
| 394 |
+
</div>
|
| 395 |
+
</div>
|
| 396 |
+
</main>
|
| 397 |
+
|
| 398 |
+
<footer>
|
| 399 |
+
<p>Image Metadata Extractor © 2025 | Extract creation timestamps and EXIF data</p>
|
| 400 |
+
</footer>
|
| 401 |
+
</div>
|
| 402 |
+
|
| 403 |
+
<script>
|
| 404 |
+
const uploadArea = document.getElementById('upload-area');
|
| 405 |
+
const fileInput = document.getElementById('file-input');
|
| 406 |
+
const uploadBtn = document.getElementById('upload-btn');
|
| 407 |
+
const resultContainer = document.getElementById('result-container');
|
| 408 |
+
const metadataContent = document.getElementById('metadata-content');
|
| 409 |
+
const resetBtn = document.getElementById('reset-btn');
|
| 410 |
+
|
| 411 |
+
// Event listeners
|
| 412 |
+
uploadBtn.addEventListener('click', () => fileInput.click());
|
| 413 |
+
fileInput.addEventListener('change', handleFileSelect);
|
| 414 |
+
uploadArea.addEventListener('dragover', handleDragOver);
|
| 415 |
+
uploadArea.addEventListener('dragleave', handleDragLeave);
|
| 416 |
+
uploadArea.addEventListener('drop', handleDrop);
|
| 417 |
+
resetBtn.addEventListener('click', resetForm);
|
| 418 |
+
|
| 419 |
+
function handleDragOver(e) {
|
| 420 |
+
e.preventDefault();
|
| 421 |
+
uploadArea.classList.add('active');
|
| 422 |
+
}
|
| 423 |
+
|
| 424 |
+
function handleDragLeave() {
|
| 425 |
+
uploadArea.classList.remove('active');
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
+
function handleDrop(e) {
|
| 429 |
+
e.preventDefault();
|
| 430 |
+
uploadArea.classList.remove('active');
|
| 431 |
+
|
| 432 |
+
if (e.dataTransfer.files.length) {
|
| 433 |
+
fileInput.files = e.dataTransfer.files;
|
| 434 |
+
processFile(fileInput.files[0]);
|
| 435 |
+
}
|
| 436 |
+
}
|
| 437 |
+
|
| 438 |
+
function handleFileSelect() {
|
| 439 |
+
if (fileInput.files.length) {
|
| 440 |
+
processFile(fileInput.files[0]);
|
| 441 |
+
}
|
| 442 |
+
}
|
| 443 |
+
|
| 444 |
+
async function processFile(file) {
|
| 445 |
+
if (!file.type.startsWith('image/')) {
|
| 446 |
+
alert('Please select an image file');
|
| 447 |
+
return;
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
const formData = new FormData();
|
| 451 |
+
formData.append('file', file);
|
| 452 |
+
|
| 453 |
+
try {
|
| 454 |
+
// Show loading state
|
| 455 |
+
uploadBtn.textContent = 'Processing...';
|
| 456 |
+
uploadBtn.disabled = true;
|
| 457 |
+
|
| 458 |
+
const response = await fetch('/extract-metadata', {
|
| 459 |
+
method: 'POST',
|
| 460 |
+
body: formData
|
| 461 |
+
});
|
| 462 |
+
|
| 463 |
+
const data = await response.json();
|
| 464 |
+
|
| 465 |
+
if (response.ok) {
|
| 466 |
+
displayMetadata(data);
|
| 467 |
+
} else {
|
| 468 |
+
showError(data.detail || 'Error processing image');
|
| 469 |
+
}
|
| 470 |
+
} catch (error) {
|
| 471 |
+
showError('Error: ' + error.message);
|
| 472 |
+
} finally {
|
| 473 |
+
uploadBtn.textContent = 'Select Image';
|
| 474 |
+
uploadBtn.disabled = false;
|
| 475 |
+
}
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
function displayMetadata(data) {
|
| 479 |
+
// Hide upload area and show results
|
| 480 |
+
uploadArea.style.display = 'none';
|
| 481 |
+
resultContainer.style.display = 'block';
|
| 482 |
+
|
| 483 |
+
// Generate metadata cards
|
| 484 |
+
let html = '';
|
| 485 |
+
|
| 486 |
+
// Basic info card
|
| 487 |
+
html += `
|
| 488 |
+
<div class="metadata-card">
|
| 489 |
+
<h3>📋 Basic Information</h3>
|
| 490 |
+
<div class="metadata-item">
|
| 491 |
+
<strong>Filename</strong>
|
| 492 |
+
<span>${data.filename}</span>
|
| 493 |
+
</div>
|
| 494 |
+
<div class="metadata-item">
|
| 495 |
+
<strong>Format</strong>
|
| 496 |
+
<span>${data.format || 'Unknown'}</span>
|
| 497 |
+
</div>
|
| 498 |
+
<div class="metadata-item">
|
| 499 |
+
<strong>Dimensions</strong>
|
| 500 |
+
<span>${data.size ? `${data.size[0]} × ${data.size[1]} pixels` : 'Unknown'}</span>
|
| 501 |
+
</div>
|
| 502 |
+
<div class="metadata-item">
|
| 503 |
+
<strong>Color Mode</strong>
|
| 504 |
+
<span>${data.mode || 'Unknown'}</span>
|
| 505 |
+
</div>
|
| 506 |
+
</div>
|
| 507 |
+
`;
|
| 508 |
+
|
| 509 |
+
// Creation date card
|
| 510 |
+
html += `
|
| 511 |
+
<div class="metadata-card creation">
|
| 512 |
+
<h3>⏱️ Creation Timestamp</h3>
|
| 513 |
+
`;
|
| 514 |
+
|
| 515 |
+
if (data.creation_date) {
|
| 516 |
+
const date = new Date(data.creation_date.date);
|
| 517 |
+
html += `
|
| 518 |
+
<div class="metadata-item">
|
| 519 |
+
<strong>Source</strong>
|
| 520 |
+
<span>${data.creation_date.source}</span>
|
| 521 |
+
</div>
|
| 522 |
+
<div class="metadata-item">
|
| 523 |
+
<strong>Date & Time</strong>
|
| 524 |
+
<span>${date.toLocaleString()}</span>
|
| 525 |
+
</div>
|
| 526 |
+
`;
|
| 527 |
+
} else if (data.file_system_creation_time) {
|
| 528 |
+
const date = new Date(data.file_system_creation_time);
|
| 529 |
+
html += `
|
| 530 |
+
<div class="metadata-item">
|
| 531 |
+
<strong>Source</strong>
|
| 532 |
+
<span>File System</span>
|
| 533 |
+
</div>
|
| 534 |
+
<div class="metadata-item">
|
| 535 |
+
<strong>Date & Time</strong>
|
| 536 |
+
<span>${date.toLocaleString()}</span>
|
| 537 |
+
</div>
|
| 538 |
+
`;
|
| 539 |
+
} else {
|
| 540 |
+
html += `
|
| 541 |
+
<div class="metadata-item error">
|
| 542 |
+
<strong>No Creation Date Found</strong>
|
| 543 |
+
<span>Neither EXIF data nor file system timestamps contain creation information</span>
|
| 544 |
+
</div>
|
| 545 |
+
`;
|
| 546 |
+
}
|
| 547 |
+
|
| 548 |
+
html += `</div>`;
|
| 549 |
+
|
| 550 |
+
// EXIF data card
|
| 551 |
+
html += `
|
| 552 |
+
<div class="metadata-card exif">
|
| 553 |
+
<h3>🔍 EXIF Metadata</h3>
|
| 554 |
+
`;
|
| 555 |
+
|
| 556 |
+
if (Object.keys(data.exif_data).length > 0) {
|
| 557 |
+
html += `<div class="exif-list">`;
|
| 558 |
+
for (const [key, value] of Object.entries(data.exif_data)) {
|
| 559 |
+
html += `
|
| 560 |
+
<div class="exif-item">
|
| 561 |
+
<span class="exif-key">${key}</span>
|
| 562 |
+
<span class="exif-value">${value}</span>
|
| 563 |
+
</div>
|
| 564 |
+
`;
|
| 565 |
+
}
|
| 566 |
+
html += `</div>`;
|
| 567 |
+
} else {
|
| 568 |
+
html += `
|
| 569 |
+
<div class="metadata-item">
|
| 570 |
+
<strong>No EXIF Data</strong>
|
| 571 |
+
<span>This image doesn't contain EXIF metadata</span>
|
| 572 |
+
</div>
|
| 573 |
+
`;
|
| 574 |
+
}
|
| 575 |
+
|
| 576 |
+
html += `</div>`;
|
| 577 |
+
|
| 578 |
+
// Error card (if any)
|
| 579 |
+
if (data.error) {
|
| 580 |
+
html += `
|
| 581 |
+
<div class="metadata-card">
|
| 582 |
+
<h3>⚠️ Error</h3>
|
| 583 |
+
<div class="metadata-item error">
|
| 584 |
+
<strong>Processing Error</strong>
|
| 585 |
+
<span>${data.error}</span>
|
| 586 |
+
</div>
|
| 587 |
+
</div>
|
| 588 |
+
`;
|
| 589 |
+
}
|
| 590 |
+
|
| 591 |
+
metadataContent.innerHTML = html;
|
| 592 |
+
}
|
| 593 |
+
|
| 594 |
+
function showError(message) {
|
| 595 |
+
// Hide upload area and show results
|
| 596 |
+
uploadArea.style.display = 'none';
|
| 597 |
+
resultContainer.style.display = 'block';
|
| 598 |
+
|
| 599 |
+
metadataContent.innerHTML = `
|
| 600 |
+
<div class="metadata-card">
|
| 601 |
+
<h3>⚠️ Error</h3>
|
| 602 |
+
<div class="metadata-item error">
|
| 603 |
+
<strong>Processing Failed</strong>
|
| 604 |
+
<span>${message}</span>
|
| 605 |
+
</div>
|
| 606 |
+
</div>
|
| 607 |
+
`;
|
| 608 |
+
}
|
| 609 |
+
|
| 610 |
+
function resetForm() {
|
| 611 |
+
// Show upload area and hide results
|
| 612 |
+
uploadArea.style.display = 'block';
|
| 613 |
+
resultContainer.style.display = 'none';
|
| 614 |
+
|
| 615 |
+
// Reset file input
|
| 616 |
+
fileInput.value = '';
|
| 617 |
+
}
|
| 618 |
+
</script>
|
| 619 |
+
</body>
|
| 620 |
+
</html>
|
| 621 |
+
"""
|
| 622 |
+
|
| 623 |
+
@app.post("/extract-metadata")
|
| 624 |
+
async def extract_metadata(file: UploadFile = File(...)):
|
| 625 |
+
"""Endpoint to extract metadata from uploaded image"""
|
| 626 |
+
if not file.content_type.startswith("image/"):
|
| 627 |
+
raise HTTPException(status_code=400, detail="File must be an image")
|
| 628 |
+
|
| 629 |
+
# Initialize tmp_file_path to None
|
| 630 |
+
tmp_file_path = None
|
| 631 |
+
|
| 632 |
+
try:
|
| 633 |
+
# Read the file content
|
| 634 |
+
contents = await file.read()
|
| 635 |
+
|
| 636 |
+
# Create a temporary file to store the uploaded image
|
| 637 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 638 |
+
tmp_file.write(contents)
|
| 639 |
+
tmp_file_path = tmp_file.name
|
| 640 |
+
|
| 641 |
+
# Extract metadata
|
| 642 |
+
metadata = get_image_metadata(tmp_file_path)
|
| 643 |
+
|
| 644 |
+
return metadata
|
| 645 |
+
except Exception as e:
|
| 646 |
+
raise HTTPException(status_code=500, detail=f"Error processing image: {str(e)}")
|
| 647 |
+
finally:
|
| 648 |
+
# Clean up temporary file
|
| 649 |
+
if tmp_file_path and os.path.exists(tmp_file_path):
|
| 650 |
+
os.unlink(tmp_file_path)
|
| 651 |
+
|
| 652 |
+
if __name__ == "__main__":
|
| 653 |
+
import uvicorn
|
|
|
|
|
|
|
|
|
|
| 654 |
uvicorn.run(app, host="0.0.0.0", port=8000)
|