From 47770eda6f77a6b1f4d64dfe05a61e75b9ed7ee7 Mon Sep 17 00:00:00 2001 From: James H Date: Sun, 28 Nov 2021 11:26:01 +0000 Subject: [PATCH] Add create_gnome_slideshow --- README.md | 16 ++++- create_gnome_slideshow.py | 121 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 create_gnome_slideshow.py diff --git a/README.md b/README.md index 7eb04b7..3897503 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,17 @@ # Python Scripts -Useful Python scripts :) \ No newline at end of file +Useful Python scripts :) + +## Scripts +### create_gnome_slideshow.py +Usage: +```bash +python3 create_gnome_slideshow.py [-r] [--name SlideshowName] [--xmlpath /path/to/save/xml] /path/to/images/folder +``` + +This script generates the XML files required to add a slideshow of images to GNOME desktop: +- An XML file declaring the slideshow name and options + - This will always be created at `$HOME/.local/share/gnome-background-properties/slideshow-name.xml` +- An XML file containin the images to use. + - This is created at the `xmlpath` argument location + - It will create an entry for all of the images in the given folder \ No newline at end of file diff --git a/create_gnome_slideshow.py b/create_gnome_slideshow.py new file mode 100644 index 0000000..08e0331 --- /dev/null +++ b/create_gnome_slideshow.py @@ -0,0 +1,121 @@ +import argparse +import os + +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})") + + 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 + + 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 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()