mczachurski / wallpapper

:computer: Console application for creating dynamic wallpapers for macOS Mojave and newer
MIT License
3.31k stars 137 forks source link

Proposal: Automated Time Based JSON Generator and GPS Adder #74

Open OWL4C opened 8 months ago

OWL4C commented 8 months ago

I just wrote a python script that adds the pictures within a folder automatically using Exif Metadata, and asks for the primary, light and dark pictures (using pythons in you can even specify only part of a pictures name and it still works) Also i added a bash script to interactively add GPS Coordinates with exiftool (which the python script depends on too), for convenience. (This is limited to only adding the GPS, so no easy cli definition of isPrimary or isForLight) Maybe others will find this of use and it can be integrated into the binary somehow, but it does require some dependencies and python, none of which are necessary for this tool. Interested to hear your thoughts. Either way adding the scripts and mentioning them in the README couldn't hurt? (Since Github does not allow for .py or .sh files, the files will be .py.txt or .sh.txt) addGPS.sh.txt createJSON.py.txt Since these are not able to be previewed, here is the source code: addGPS.sh

#/bin/bash
echo "GPS Coordinates can be supplied in decimal and x°y'z\" form. By default you are entering North-East, but by prefixing with '-' you can swith to West or South"

read -p "Enter a Latitude: " Latitude
echo "Latitude: $Latitude"

read -p "Enter a Longitude: " Longitude
echo "Longitude: $Longitude"

read -p "Enter the Pictures (* notation works): " Pictures
echo "You entered $Pictures"

exiftool -XMP:GPSLongitude=$Longitude -XMP:GPSLatitude=$Latitude -GPSLongitudeRef="East" -GPSLatitudeRef="North" $Pictures

and createJSON.py

# This tool depends on pyexiftool, download it with "pip install PyExifTool"
import os
import exiftool
import json

outputjson=[] # This will be filled with the entries

def extract_date(picture):
    with exiftool.ExifToolHelper() as et:
        try:
            metadata = et.get_metadata(picture)
            for d in metadata:
                return (d["EXIF:DateTimeOriginal"].replace(":","-").replace(" ","T")+"Z")
        except:
            return "MissingExif"

# Converts the given path to an absolute path, to prevent File Errors
dirpath=input("Picture Directory (. for here): ");
dirpath=os.path.abspath(dirpath)+"/" 
# Asks for the Special Pictures. Any string that python can match to exactly one picture works. (It seems that multiple matches do not affect functionality of wallpapper, but this will create ambiguity.)
primary=input("Thumbnail: ")
daypath=input("LightMode: ")
nightpath=input("DarkMode: ")

for file in os.listdir(dirpath):
    # Only accept pictures of this format. Currently limited, other formats should work too
    if file.split(".")[-1].lower() in ["jpg","png"]: 
        # Adds Entry for all pictures with fileName and time
        out_dict={"fileName": dirpath+file,"time": extract_date(dirpath+file)} 
        # Adds the relevant special entries to matching pictures
        if primary in file:
            out_dict["isPrimary"]=True
        if nightpath in file:
            out_dict["isForDark"]=True
        if daypath in file:
            out_dict["isForLight"]=True

        outputjson.append(out_dict)

# Writes out the file 
with open("output.json","w") as output:
    output.write(json.dumps(outputjson))
print(outputjson)