【问题标题】:Unix change desktop background seamlesslyUnix无缝更改桌面背景
【发布时间】:2013-08-11 14:40:50
【问题描述】:

所以我有一个 python 脚本,它生成一个图像,并保存过去作为背景图像的旧图像。

我尝试使用 crontab 让它运行,但无法让它工作,所以现在我只有一个 bash 脚本,它在我的 .bashrc 中运行一次,当我第一次登录时(我有一个 @987654323 @ 里面的东西)。

问题是,当背景更新时,它会不时闪烁黑色 - 这不是很好!

我目前让它每秒运行一次,但我不认为是 python 导致了黑屏,更多的是图像转换的方式......

有什么方法可以防止更新之间出现这些丑陋的黑屏吗?

这是运行它的所有代码,如果你想尝试一下......

从 PIL 导入 Image、ImageDraw、ImageFilter 导入颜色系统 从随机导入高斯

xSize, ySize = 1600,900

im = Image.new('RGBA', (xSize, ySize), (0, 0, 0, 0)) 
draw = ImageDraw.Draw(im) 

class Cube(object):
    def __init__(self):
        self.tl = (0,0)
        self.tm = (0,0)
        self.tr = (0,0)
        self.tb = (0,0)
        self.bl = (0,0)
        self.bm = (0,0)
        self.br = (0,0)

    def intify(self):
        for prop in [self.tl, self.tm, self.tr, self.tb, self.bl, self.bm, self.br]:
            prop = [int(i) for i in prop]

def drawCube((x,y), size, colour=(255,0,0)):

    p = Cube()

    colours = [list(colorsys.rgb_to_hls(*[c/255.0 for c in colour])) for _ in range(3)]
    colours[0][1] -= 0
    colours[1][1] -= 0.2
    colours[2][1] -= 0.4
    colours = [tuple([int(i*255) for i in colorsys.hls_to_rgb(*colour)]) for colour in colours]


    p.tl = x,y #Top Left
    p.tm = x+size/2, y-size/4 #Top Middle
    p.tr = x+size, y #Top Right
    p.tb = x+size/2, y+size/4 #Top Bottom
    p.bl = x, y+size/2 #Bottom Left
    p.bm = x+size/2, y+size*3/4 #Bottom Middle
    p.br = x+size, y+size/2 #Bottom Right

    p.intify()

    draw.polygon((p.tl, p.tm, p.tr, p.tb), fill=colours[0])
    draw.polygon((p.tl, p.bl, p.bm, p.tb), fill=colours[1])
    draw.polygon((p.tb, p.tr, p.br, p.bm), fill=colours[2])

    lineColour = (0,0,0)
    lineThickness = 2

    draw.line((p.tl, p.tm), fill=lineColour, width=lineThickness)
    draw.line((p.tl, p.tb), fill=lineColour, width=lineThickness)
    draw.line((p.tm, p.tr), fill=lineColour, width=lineThickness)
    draw.line((p.tb, p.tr), fill=lineColour, width=lineThickness)
    draw.line((p.tl, p.bl), fill=lineColour, width=lineThickness)
    draw.line((p.tb, p.bm), fill=lineColour, width=lineThickness)
    draw.line((p.tr, p.br), fill=lineColour, width=lineThickness)
    draw.line((p.bl, p.bm), fill=lineColour, width=lineThickness)
    draw.line((p.bm, p.br), fill=lineColour, width=lineThickness)


# -------- Actually do the drawing

size = 100

#Read in file of all colours, and random walk them
with open("/home/will/Documents/python/cubeWall/oldColours.dat") as coloursFile:
    for line in coloursFile:
        oldColours = [int(i) for i in line.split()]

oldColours = [int(round(c + gauss(0,1.5)))%255 for c in oldColours]


colours = [[ int(c*255) for c in colorsys.hsv_to_rgb(i/255.0, 1, 1)] for i in oldColours]


with open("/home/will/Documents/python/cubeWall/oldColours.dat", "w") as coloursFile:
    coloursFile.write(" ".join([str(i) for i in oldColours]) + "\n")


for i in range(xSize/size+2):
    for j in range(2*ySize/size+2):
        if j%3 == 0:
            drawCube((i*size,j*size/2), size, colour=colours[(i+j)%3])
        elif j%3 == 1:
            drawCube(((i-0.5)*size,(0.5*j+0.25)*size), size, colour=colours[(i+j)%3])


im2 = im.filter(ImageFilter.SMOOTH)
im2.save("cubes.png")
#im2.show()

然后运行这个:

#!/bin/sh

while [ 1 ]
do
    python drawCubes.py
sleep 1
done

并设置桌面图片为cubes.png

【问题讨论】:

    标签: python bash unix cron python-imaging-library


    【解决方案1】:

    好吧,您可以通过运行来更改当前的wallaper(在Gnome 3 兼容的桌面中)

    import os
    os.system("gsettings set org.gnome.desktop.background picture-uri file://%(path)s" % {'path':absolute_path})
    os.system("gsettings set org.gnome.desktop.background picture-options wallpaper")
    

    【讨论】:

    • 您认为这会比仅仅更改当前用于后台的文件并让它自行更新更好吗?
    • 实际上,仅覆盖文件可能是最糟糕的方法。通过使用 gsettings(未经测试),您有更多机会看到图片之间的平滑淡入淡出。
    • 是的,我意识到事后可能会出现这种情况,gsettings 肯定会发出某种信号。我会试一试。干杯。
    • 好的,所以我尝试了这个(只是在命令行中使用gsettings),但它没有用。如果我运行gnome-session --version 我会得到3.2.1...有什么想法吗?
    • 如果文件 URI 包含空格,您可能需要用双引号将其括起来。例如。 gsettings set org.gnome.desktop.background picture-uri "file:///home/steve/Images/wallpaper/Almora_mbr/Almora_2560x1600_Grain.jpg"
    【解决方案2】:

    如果您使用的是 MATE,那么您使用的是 Gnome 2.x 的一个分支。我为 Gnome 2 找到的方法是:gconftool-2 --set --type string --set /desktop/gnome/background/picture_filename <absolute image path>

    我们之前尝试过的方法可以在 Gnome Shell 中使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2015-10-04
      相关资源
      最近更新 更多