如何在 Xfce 中根据壁纸颜色设置背景颜色?

如何在 Xfce 中根据壁纸颜色设置背景颜色?

我有一个文件夹,里面有数百张(可能数千张)图片,我用它来循环显示 Xfce 中的壁纸。唯一的问题是,当图片设置为“缩放”时,它们看起来最好,对于某些图片,这会产生“信箱”效果,即用纯色背景填充其余区域。

我的问题是,上述背景颜色是否可以随着图像动态变化,使其看起来不那么空洞并与图像相适应,例如有多少漫画观众喜欢麦克米克斯怎么做?如果您不明白我在说什么,简短的解释是:如果图像大部分是白色,我希望纯色背景颜色为白色;如果图像大部分是黑色,我希望纯色背景颜色为黑色;等等。

答案1

经过一番思考,我决定编写一个 Python 脚本(Python 3),使用我找到的一些信息来监控last-image这个方便的实用程序的变化xfconf-query这里(稍微修改一下,只获取边框像素)。

您需要安装(最好使用 pip)numpy 和 Pillow:

pip3 install Pillow pip3 install numpy

接下来,创建一个包含此脚本的 .py 文件;我将其命名为“change-bg-with-color.py”:

#!/usr/bin/python3
from PIL import Image
from subprocess import Popen, PIPE
import numpy as np
import os
import traceback

# Edit to point to your workspace
WORKSPACE = "/backdrop/screen0/monitor2/workspace0"

# Choose your flavor! Average...
def compute_average_image_color(img):
    width, height = img.size

    r_total = 0
    g_total = 0
    b_total = 0
    a_total = 0

    count = 0

    # Get top and bottom borders
    for y in [0,height-1]:
        for x in range(0, width):
            r, g, b, a = img.getpixel((x,y))
            r_total += r
            g_total += g
            b_total += b
            a_total += a
            count += 1

    # Get left and right borders
    for x in [0,width-1]:
        for y in range(0, height):
            r, g, b, a = img.getpixel((x,y))
            r_total += r
            g_total += g
            b_total += b
            a_total += a
            count += 1

    return (np.uint16(r_total/count * 65535.0/255.0), np.uint16(g_total/count * 65535.0/255.0), np.uint16(b_total/count * 65535.0/255.0), np.uint16(a_total/count * 65535.0/255.0))

# or Mode
def compute_mode_image_color(img):
    width, height = img.size

    pixel_bins = {}

    # Get top and bottom borders
    for y in [0,height-1]:
        for x in range(0, width):
            pixel = img.getpixel((x,y))

            if pixel in pixel_bins:
                pixel_bins[pixel] += 1
            else:
                pixel_bins[pixel] = 1

    # Get left and right borders
    for x in [0,width-1]:
        for y in range(0, height):
            pixel = img.getpixel((x,y))

            if pixel in pixel_bins:
                pixel_bins[pixel] += 1
            else:
                pixel_bins[pixel] = 1

    pixel = (255,255,255,255)
    mode = 0
    for p,m in pixel_bins.items():
        if m > mode:
            pixel = p

    return (np.uint16(pixel[0] * 65535.0/255.0), np.uint16(pixel[1] * 65535.0/255.0), np.uint16(pixel[2] * 65535.0/255.0), np.uint16(pixel[3] * 65535.0/255.0))

# Start the monitor for changes to last-image
process = Popen(["xfconf-query", "-c", "xfce4-desktop", "-p", os.path.join(WORKSPACE, "last-image"), "-m"], stdout=PIPE)
while True:
    try:
        # Get the initial BG image from the workspace
        p2 = Popen(["xfconf-query", "-c", "xfce4-desktop", "-p", os.path.join(WORKSPACE, "last-image")], stdout=PIPE)
        (filename, err) = p2.communicate()
        exit_code = p2.wait()

        # Next, open the image
        img = Image.open(filename.decode('utf-8').strip()).convert("RGBA")

        # Determine and set the color (CHOOSE YOUR FLAVOR HERE)
        color = compute_mode_image_color(img)
        p2 = Popen(["xfconf-query", "-c", "xfce4-desktop", "-p", os.path.join(WORKSPACE, "color1"), "-s", str(color[0]) , "-s", str(color[1]), "-s", str(color[2]), "-s", str(color[3])], stdout=PIPE)
        (output, err) = p2.communicate()
        p2.wait()

        # Wait for next line
        line = process.stdout.readline()
        if line == '' and process.poll() is not None:
            break
    except Exception as e:
        print(e)
        traceback.print_exc()
        pass

选择您的风格(平均或模式)。务必修改字段WORKSPACE以指向您的工作区。您通常可以通过查看 ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-desktop.xml 找到它(感谢 Dial!)

只需运行脚本,背景颜色就会立即改变。当然,您可以将其配置为在启动时运行,但为了简单起见,省略了这些细节。这对我来说很有效!

相关内容