Add create_gnome_slideshow
This commit is contained in:
16
README.md
16
README.md
@@ -1,3 +1,17 @@
|
||||
# Python Scripts
|
||||
|
||||
Useful Python scripts :)
|
||||
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
|
||||
121
create_gnome_slideshow.py
Normal file
121
create_gnome_slideshow.py
Normal file
@@ -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('<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE wallpapers SYSTEM "gnome-wp-list.dtd">'.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('<?xml version="1.0" ?>'.encode("UTF-8"))
|
||||
|
||||
xmlfile.write(ET.tostring(background))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user