import os
from PIL.ExifTags import TAGS

import ast

from PIL import Image, ImageDraw


def create_image_with_dots(background_image_path, width, height, coordinates, dot_size=5, output_file="output.jpg"):
     # Open the existing image
    image = Image.open(background_image_path)
    
    # Get the dimensions of the image
    width, height = image.size
    # Create a drawing object
    draw = ImageDraw.Draw(image)
    
    # Draw black dots at the specified coordinates
    for dot_x, dot_y in coordinates:
        draw.ellipse([((dot_x*width/360) + width/2 - dot_size//2, (dot_y*height/180) + height/2 - dot_size//2), 
                      ((dot_x*width/360) + width/2 + dot_size//2, (dot_y*height/180) + height/2 + dot_size//2)], 
                     fill="yellow")
    
    # Save the image
    image.save(output_file, "JPEG")
    print(f"Image saved as {output_file}")



def parse_gps_metadata(exif_data):
    # Convert the string representation of the dictionary to an actual dictionary
    metadata = ast.literal_eval(exif_data)
    
    def extract_datapoint(key):
        return metadata.get(key, None)
    
    return extract_datapoint

coordinates=[]

def get_image_properties(file_path):
    try:
        with Image.open(file_path) as img:
  
            if hasattr(img, '_getexif'):
                exif_data = img._getexif()
                print(exif_data)
                latitude = exif_data[34853][2]
                longitude = exif_data[34853][4]

                latitudenum=float(latitude[0])+float(latitude[1])/60+float(latitude[2])/3600
                longitudenum=float(longitude[0])+float(longitude[1])/60+float(longitude[2])/3600
                if exif_data[34853][3]=='E':
                    longitudenum=longitudenum
                if exif_data[34853][3]=='W':
                    longitudenum=longitudenum*float(-1)
                if exif_data[34853][1]=='S':
                    latitudenum=latitudenum
                if exif_data[34853][1]=='N':
                    latitudenum=latitudenum*float(-1)

                print(f'Longitude:{longitudenum}')
                print(f'Latitude:{latitudenum}')
                coordinate= longitudenum, latitudenum
                print(f'coordinate {coordinate}')
                coordinates.append(coordinate)
                print(f'coordinates: {coordinates}')
               

    except Exception as e:
        print(f"Error processing {file_path}: {str(e)}")
        return None

def main(folder_path):
    image_data = []
    
    for filename in os.listdir(folder_path):
        if filename.lower().endswith('.jpg') or filename.lower().endswith('.jpeg'):
            file_path = os.path.join(folder_path, filename)
            properties = get_image_properties(file_path)
            if properties:
                image_data.append(properties)
    
    create_image_with_dots('foto/background.jpg',8000, 6000, coordinates) #background image: map3.jpg
    
if __name__ == "__main__":
    folder_path = input("Enter the folder path containing JPG files: ")
    main(folder_path)