import argparse import os import random import xml.etree.ElementTree as ET ALLOWED_EXTENSIONS = [".jpg", ".jpeg", ".png"] DEFAULT_DURATION = 120 DEFAULT_TRANSITION_DURATION = 0.5 GNOME_BACKGROUNDS_PATH = "$HOME/.local/share/gnome-background-properties" def getFiles(path : str, recursive : bool) -> list: files = [] with os.scandir(path) as it: for entry in it: if entry.is_file(): if (os.path.splitext(entry.name)[1] in ALLOWED_EXTENSIONS): files.append(f"{path}/{entry.name}") elif entry.is_dir() and recursive: files.append(getFiles(f"{path}/{entry.name}")) return files def main(): parser = argparse.ArgumentParser(description="Generate the XML files required to add a slideshow of images to GNOME desktop.") parser.add_argument("path", metavar="PATH", type=str, help="Path to the images directory to create a slideshow for") parser.add_argument("--recursive", "-r", action="store_true", help="Recursively search subdirectories for images") parser.add_argument("--name", "-n", type=str, help="Name of the slideshow") parser.add_argument("--xmlpath", type=str, help="Path to save the XML file containing the list of image files. (default: stored in the images folder)") parser.add_argument("--duration", "-d", type=int, help=f"Duration between each image in seconds (default: {DEFAULT_DURATION})") parser.add_argument("--transduration", "-t", type=int, help=f"Duration of transition between images (default: {DEFAULT_TRANSITION_DURATION})") parser.add_argument("-s", "--shuffle", action="store_true", help="Turn on shuffling of images") args = parser.parse_args() imagepath = os.path.abspath(args.path) xmlpath = args.xmlpath slideshowname = args.name recursive = args.recursive duration = args.duration transduration = args.transduration shuffle = args.shuffle gnomepath = os.path.abspath(os.path.expandvars(GNOME_BACKGROUNDS_PATH)) if slideshowname == None: slideshowname = os.path.dirname(imagepath).split("/")[-1] if xmlpath == None: xmlpath = imagepath + f"/{slideshowname}.xml" if duration == None: duration = DEFAULT_DURATION if transduration == None: transduration = DEFAULT_TRANSITION_DURATION print(f"imagepath : {imagepath}") print(f"xmlpath : {xmlpath}") print(f"slideshowname: {slideshowname}") print(f"recursive : {recursive}") print(f"duration : {duration}") print(f"transduration: {transduration}") files = getFiles(imagepath, recursive) if shuffle: random.shuffle(files) if not os.path.exists(gnomepath): os.makedirs(gnomepath) with open(f"{gnomepath}/{slideshowname}.xml", "wb") as xmlfile: # Generate XML file wallpapers = ET.Element("wallpapers") wallpaper = ET.SubElement(wallpapers, "wallpaper", { "deleted": "false" }) name = ET.SubElement(wallpaper, "name") name.text = slideshowname filename = ET.SubElement(wallpaper, "filename") filename.text = xmlpath options = ET.SubElement(wallpaper, "options") options.text = "zoom" xmlfile.write(''.encode("UTF-8")) xmlfile.write(ET.tostring(wallpapers)) with open(xmlpath, "wb") as xmlfile: background = ET.Element("background") for i in range(len(files)): file = files[i] tofile = files[(i + 1) % len(files)] fromfile = file static = ET.SubElement(background, "static") durationelement = ET.SubElement(static, "duration") durationelement.text = str(duration) fileelement = ET.SubElement(static, "file") fileelement.text = file transition = ET.SubElement(background, "transition") transdurationelement = ET.SubElement(transition, "duration") transdurationelement.text = str(transduration) fromelement = ET.SubElement(transition, "from") fromelement.text = fromfile toelement = ET.SubElement(transition, "to") toelement.text = tofile xmlfile.write(''.encode("UTF-8")) xmlfile.write(ET.tostring(background)) if __name__ == "__main__": main()