Ubuntu 24 并不支持多显示器设置不同壁纸,但是可以通过设置一个大的拉伸壁纸来实现这个效果。
效果图

设置壁纸方法
提示:可能需要安装 dconf-editor 工具,可以通过 sudo apt install dconf-editor 安装。
import os
def set_wallpaper(path, mode="zoom"):
cmd = f"""
dconf write /org/gnome/desktop/background/picture-uri-dark "'file://{path}'"
dconf write /org/gnome/desktop/background/picture-uri "'file://{path}'"
dconf write /org/gnome/desktop/background/picture-options "'{mode}'"
"""
os.system(cmd)
横向拼接多张图片
from PIL import Image
def horizontal_concatenate(images):
"""
横向拼接多张图片。
:param images: 图片文件路径的列表
:return: 拼接后的图片对象
"""
# 打开所有图片
img_list = [Image.open(img) for img in images]
# 获取图片的宽度和高度
widths, heights = zip(*(img.size for img in img_list))
# 计算拼接后图片的总宽度和最大高度
total_width = sum(widths)
max_height = max(heights)
# 创建一个新的空白图片,大小为(总宽度, 最大高度)
new_img = Image.new("RGB", (total_width, max_height))
# 将每张图片粘贴到新图片上
x_offset = 0 # 当前的横向偏移量
for img in img_list:
new_img.paste(img, (x_offset, 0))
x_offset += img.size[0] # 更新偏移量
return new_img
使用示例
def main():
# 三张图片的路径
image_paths = [
"/home/ogumo/文档/GitHub/Multiple-Monitors-Wallpaper/img/1.jpg",
"/home/ogumo/文档/GitHub/Multiple-Monitors-Wallpaper/img/2.png",
"/home/ogumo/文档/GitHub/Multiple-Monitors-Wallpaper/img/3.jpg",
]
# 横向拼接图片
result = horizontal_concatenate(image_paths)
# 保存结果
result.save("result.png")
# result.show() # 显示拼接后的图片
path = os.path.join(os.getcwd(), "/home/ogumo/文档/GitHub/Multiple-Monitors-Wallpaper/result.png")
set_wallpaper(path, "spanned")
if __name__ == "__main__":
main()