Last active
October 28, 2018 16:59
-
-
Save cormoran/f8fbac4ba0ab761f2234d74b76922c30 to your computer and use it in GitHub Desktop.
gnome3 の live wallpaper 用 xml を作るスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# usage: ./generate_wallpaper_xml.py [画像のあるフォルダ] [出力するxmlのパス] | |
# フォルダを再帰的に検索して, | |
# <適当な名前>_<表示するduration(sec)>.(png|jpeg|jpg) | |
# にマッチするファイルを見つけてランダムに並べて wallpaper.xml を作る | |
# | |
# 壁紙に設定するには | |
# /usr/bin/gsettings set org.gnome.desktop.background picture-uri 'file:///path/to/wallpaper.xml' | |
import os, sys, glob, re, random | |
def find_images(dir_path): | |
exts = ['png', 'jpg', 'jpeg'] | |
pattern = re.compile('^.*_(\d+)\.({})$'.format('|'.join(exts))) | |
images = [] | |
for ext in exts: | |
images.extend( | |
glob.glob(os.path.join(dir_path, '**', '*'), recursive=True)) | |
images_with_time = [] | |
for image in images: | |
match = pattern.match(image) | |
if match and int(match.group(1)) > 0: | |
images_with_time.append(( | |
image, | |
int(match.group(1)), | |
)) | |
return images_with_time | |
def generate_wallpaper_xml(image_dir, output_path): | |
images_with_time = sorted(find_images(image_dir)) | |
print('found:') | |
for i, t in images_with_time: | |
print('{:6} sec {}'.format(t, i)) | |
random.shuffle(images_with_time) | |
def add_static(xml, i, t): | |
# yapf: disable | |
static = [ | |
'<static>', | |
' <duration>{}</duration>'.format(t), | |
' <file>{}</file>'.format(i), | |
'</static>', | |
] | |
# yapf: enable | |
xml.extend(map(lambda s: ' ' + s, static)) | |
xml = ['<background>'] | |
for i, t in images_with_time: | |
add_static(xml, os.path.abspath(i), t) | |
xml.append('</background>') | |
print('\n'.join(xml), file=open(output_path, 'w')) | |
if __name__ == '__main__': | |
image_dir = sys.argv[1] if len(sys.argv) > 1 else '.' | |
output_path = sys.argv[2] if len(sys.argv) > 2 else './wallpaper.xml' | |
generate_wallpaper_xml(image_dir, output_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment