吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1227|回复: 3
收起左侧

[Python 转载] Ubuntu写python脚本实现自定义壁纸幻灯片:Vue法、字符串拼接法、minidom法

  [复制链接]
hans7 发表于 2022-8-7 18:39
本帖最后由 hans7 于 2022-8-9 01:58 编辑

环境

Windows10、虚拟机Ubuntu20.04

本文juejin:https://juejin.cn/post/7129080519945355277/

本文52pojie:https://www.52pojie.cn/thread-1672344-1-1.html

本文CSDN:https://blog.csdn.net/hans774882968/article/details/126215309

作者:hans774882968以及hans774882968

配置文件何在

查阅资料可知,控制壁纸幻灯片的文件是:/usr/share/backgrounds/contest/focal.xml

如果权限不够,就修改一下:

sudo chmod 777 focal.xml

配置文件写法

原有内容结构如下:

<background>
  <starttime>
    <year>2020</year>
    <month>04</month>
    <day>01</day>
    <hour>00</hour>
    <minute>00</minute>
    <second>00</second>
  </starttime>
  <!-- This animation will start at midnight. -->
  <!-- 剩下的是主体部分,省略 -->
</background>

观察原有内容,我们归纳得出,控制一张图片的xml如下:

  <static>
    <duration>25.0</duration>
    <file>/home/xxx/图片/壁纸1.png</file>
  </static>
  <transition>
    <duration>2.0</duration>
    <from>/home/xxx/图片/壁纸1.png</from>
    <to>/home/xxx/图片/壁纸2.png</to>
  </transition>

因此,我们只需要提供:static持续的时间、transition持续的时间(均以秒为单位)、当前图片和下一张图片。

脚本

Vue也可以很方便地生成xml

传送门

字符串拼接版

关于脚本:

  1. file_template.xml的主体部分,用%s表示。求好主体部分imgs_xml后,用file_template % imgs_xml来添加。
  2. 注意编码指定为utf-8
  3. 这种直接拼接字符串的方式保证缩进完全正确是比较麻烦的。可以试试用minidom再实现一次,对比实现难度。

使用时:

  1. 只需要修改files数组。
  2. 运行完毕,文件生成后,自行替换/usr/share/backgrounds/contest/focal.xml的原有内容。
  3. 修改后不会立刻生效,目前我是用重启解决这个问题的。如果能找到对应的进程,手动重启它,就更好了。
def get_file_template():
    import sys
    try:
        with open('file_template.xml', 'r', encoding='utf-8') as f:
            return f.read().strip()
    except Exception as e:
        print(e)
        sys.exit(-1)

def get_imgs_xml():
    static_duration = 25.0
    transition_duration = 2.0
    files = [
        # 你的图片的绝对路径,如'/home/xxx/图片/1.png'
    ]
    one_img_template = '''
  <static>
    <duration>{static_duration}</duration>
    <file>{cur_path}</file>
  </static>
  <transition>
    <duration>{transition_duration}</duration>
    <from>{cur_path}</from>
    <to>{next_path}</to>
  </transition>
'''.lstrip('\n')
    a = []
    for i, cur_path in enumerate(files):
        next_path = files[(i + 1) % len(files)]
        a.append(one_img_template.format(
            cur_path=cur_path, next_path=next_path,
            static_duration=static_duration,
            transition_duration=transition_duration
        ))
    return ''.join(a)

if __name__ == '__main__':
    file_template = get_file_template()
    imgs_xml = get_imgs_xml()
    content = file_template % imgs_xml
    with open('focal.xml', 'w', encoding='utf-8') as f:
        f.write(content)

minidom版

命令式地操作xml,不符合直觉,感觉更难写!

from xml.dom.minidom import Document

def txt_node(txt):
    return doc.createTextNode(str(txt))

def sub_node(name, text):
    txt_nd = txt_node(text)
    node = doc.createElement(name)
    node.appendChild(txt_nd)
    return node

doc = Document()
root = doc.createElement('background')
doc.appendChild(root)
start_time = doc.createElement('starttime')
time_arr = [
    ('year', '2020'), ('month', '04'), ('day', '01'),
    ('hour', '00'), ('minute', '00'), ('second', '00')
]
for t in time_arr:
    txt = txt_node(t[1])
    t_node = doc.createElement(t[0])
    t_node.appendChild(txt)
    start_time.appendChild(t_node)
root.appendChild(start_time)

static_duration = 25.0
transition_duration = 2.0
files = [
    # 你的图片的绝对路径,如'/home/xxx/图片/1.png'
]
for i, cur_path in enumerate(files):
    # static node
    next_path = files[(i + 1) % len(files)]
    duration_node = sub_node('duration', static_duration)
    file_node = sub_node('file', cur_path)
    static_node = doc.createElement('static')
    static_node.appendChild(duration_node)
    static_node.appendChild(file_node)
    # transition node
    duration_node = sub_node('duration', transition_duration)
    from_node = sub_node('from', cur_path)
    to_node = sub_node('to', next_path)
    transition_node = doc.createElement('transition')
    transition_node.appendChild(duration_node)
    transition_node.appendChild(from_node)
    transition_node.appendChild(to_node)

    root.appendChild(static_node)
    root.appendChild(transition_node)
with open('focal_minidom.xml', 'w', encoding='utf-8') as f:
    f.write(doc.toprettyxml(indent='  ', encoding='utf-8').decode('utf-8'))

免费评分

参与人数 1吾爱币 +1 收起 理由
weidechan + 1 用心讨论,共获提升!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

tlpking 发表于 2022-8-8 07:48
思维敏捷,高手
唐子啊 发表于 2022-8-8 10:20
virsnow 发表于 2022-10-2 15:09
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 21:03

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表