0

所以我有一个生成图像的python脚本,并保存了曾经是背景图像的旧图像。

我试图让它运行使用crontab,但无法让它工作,所以现在我只有一个 bash 脚本,它在我.bashrc第一次登录时运行一次(我if [ firstRun ]在那里有一种东西)。

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

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

有没有办法可以防止更新之间出现这些丑陋的黑屏?

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

from PIL import Image, ImageDraw, ImageFilter import colorsys from random import gauss

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

4

2 回答 2

2

好吧,您可以通过运行

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")
于 2013-08-12T06:41:26.267 回答
1

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

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

于 2013-08-13T13:17:06.507 回答