Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import piexif
|
4 |
+
import piexif.helper
|
5 |
+
from datetime import datetime
|
6 |
+
import os
|
7 |
+
|
8 |
+
def add_360_metadata(input_image):
|
9 |
+
"""Add 360 photo metadata to an image file."""
|
10 |
+
try:
|
11 |
+
# Open the image
|
12 |
+
img = Image.open(input_image)
|
13 |
+
|
14 |
+
# Prepare XMP metadata for 360 photo
|
15 |
+
xmp_metadata = (
|
16 |
+
'<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>\n'
|
17 |
+
'<x:xmpmeta xmlns:x="adobe:ns:meta/">\n'
|
18 |
+
'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">\n'
|
19 |
+
'<rdf:Description rdf:about="" xmlns:GPano="http://ns.google.com/photos/1.0/panorama/">\n'
|
20 |
+
'<GPano:ProjectionType>equirectangular</GPano:ProjectionType>\n'
|
21 |
+
'<GPano:UsePanoramaViewer>True</GPano:UsePanoramaViewer>\n'
|
22 |
+
'<GPano:CroppedAreaImageWidthPixels>' + str(img.width) + '</GPano:CroppedAreaImageWidthPixels>\n'
|
23 |
+
'<GPano:CroppedAreaImageHeightPixels>' + str(img.height) + '</GPano:CroppedAreaImageHeightPixels>\n'
|
24 |
+
'<GPano:FullPanoWidthPixels>' + str(img.width) + '</GPano:FullPanoWidthPixels>\n'
|
25 |
+
'<GPano:FullPanoHeightPixels>' + str(img.height) + '</GPano:FullPanoHeightPixels>\n'
|
26 |
+
'<GPano:CroppedAreaLeftPixels>0</GPano:CroppedAreaLeftPixels>\n'
|
27 |
+
'<GPano:CroppedAreaTopPixels>0</GPano:CroppedAreaTopPixels>\n'
|
28 |
+
'</rdf:Description>\n'
|
29 |
+
'</rdf:RDF>\n'
|
30 |
+
'</x:xmpmeta>\n'
|
31 |
+
'<?xpacket end="w"?>'
|
32 |
+
)
|
33 |
+
|
34 |
+
# Create temporary filename for output
|
35 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
36 |
+
output_filename = f"360_photo_{timestamp}.jpg"
|
37 |
+
|
38 |
+
# Create EXIF dictionary with XMP metadata
|
39 |
+
exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None}
|
40 |
+
exif_dict["0th"][piexif.ImageIFD.XPComment] = xmp_metadata.encode('utf-16')
|
41 |
+
|
42 |
+
# Convert EXIF dictionary to bytes
|
43 |
+
exif_bytes = piexif.dump(exif_dict)
|
44 |
+
|
45 |
+
# Save image with metadata
|
46 |
+
output_path = os.path.join("/tmp", output_filename)
|
47 |
+
img.save(output_path, "JPEG", exif=exif_bytes, quality=95)
|
48 |
+
|
49 |
+
return output_path
|
50 |
+
|
51 |
+
except Exception as e:
|
52 |
+
raise gr.Error(f"Error processing image: {str(e)}")
|
53 |
+
|
54 |
+
# Create Gradio interface
|
55 |
+
iface = gr.Interface(
|
56 |
+
fn=add_360_metadata,
|
57 |
+
inputs=gr.Image(type="filepath", label="Upload 360° Photo"),
|
58 |
+
outputs=gr.Image(type="filepath", label="360° Photo with Metadata"),
|
59 |
+
title="360° Photo Metadata Adder",
|
60 |
+
description="Upload an equirectangular 360° photo to add necessary metadata for Google Photos and other 360° viewers. Your image should have a 2:1 aspect ratio for best results.",
|
61 |
+
examples=[],
|
62 |
+
cache_examples=False
|
63 |
+
)
|
64 |
+
|
65 |
+
# Launch the interface
|
66 |
+
iface.launch()
|