如何在Xfce中根據壁紙顏色設定背景顏色?

如何在Xfce中根據壁紙顏色設定背景顏色?

我有一個資料夾,裡面有數百(可能數千)張圖像,我用它來循環 Xfce 中的壁紙。唯一的問題是,當影像設定為「縮放」時,它們看起來最好,對於某些影像來說,這將具有「信箱」效果,用純背景色填滿區域的其餘部分。

我的問題是,所述背景顏色是否可以隨著圖像動態變化,使其看起來不那麼空虛並適合圖像,例如有多少漫畫觀眾喜歡麥克米克斯做?如果你不知道我在說什麼,簡短的解釋是:如果圖像大部分是白色,我希望純色背景顏色是白色;如果影像大部分是黑色,我希望純色背景顏色是黑色; ETC。

答案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!)

只需運行腳本,然後“砰”的一聲,背景顏色就會立即改變。您當然可以將其配置為在啟動時運行,但為了簡單起見,省略了這些細節。這對我有用!

相關內容