import gradio as gr from PIL import Image import piexif import piexif.helper from datetime import datetime import os def add_360_metadata(input_image): """Add 360 photo metadata to an image file.""" try: # Open the image img = Image.open(input_image) # Prepare XMP metadata for 360 photo xmp_metadata = ( '\n' '\n' '\n' '\n' 'equirectangular\n' 'True\n' '' + str(img.width) + '\n' '' + str(img.height) + '\n' '' + str(img.width) + '\n' '' + str(img.height) + '\n' '0\n' '0\n' '\n' '\n' '\n' '' ) # Create temporary filename for output timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output_filename = f"360_photo_{timestamp}.jpg" # Create EXIF dictionary with XMP metadata exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None} exif_dict["0th"][piexif.ImageIFD.XPComment] = xmp_metadata.encode('utf-16') # Convert EXIF dictionary to bytes exif_bytes = piexif.dump(exif_dict) # Save image with metadata output_path = os.path.join("/tmp", output_filename) img.save(output_path, "JPEG", exif=exif_bytes, quality=95) return output_path except Exception as e: raise gr.Error(f"Error processing image: {str(e)}") # Create Gradio interface iface = gr.Interface( fn=add_360_metadata, inputs=gr.Image(type="filepath", label="Upload 360° Photo"), outputs=gr.Image(type="filepath", label="360° Photo with Metadata"), title="360° Photo Metadata Adder", 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.", examples=[], cache_examples=False ) # Launch the interface iface.launch()