简体   繁体   English

从模板构建文件并添加调整(将图像转换为conky配置)

[英]Build files from template and add tweaks (turn images into conky configs)

I found very good Pinterest pictures which I want to randomly show on my desktop. 我找到了很好的Pinterest图片,想要随机显示在桌面上。 I already have a script(s) that randomly call conky configs from a directory. 我已经有一个脚本,可以从目录中随机调用conky配置。 (Then I manually make bash scripts that call from 1 to 3+ conkies at a time) (然后,我手动制作bash脚本,一次调用1至3个以上的conkis)

Tried to do some python scripting but it didn't... I over complicated it. 试图做一些python脚本,但是没有……我太复杂了。 Here's the GIT FILES : 这是GIT文件

I was planning to show my code but it's too big and messy. 我本来打算展示我的代码,但是它又太大又杂乱。

Originally I just read all the images and their Width, Height, Path and File name to separate txt files. 最初,我只是读取所有图像及其宽度,高度,路径和文件名来分隔txt文件。 Like width.txt, path.txt,etc. 像width.txt,path.txt等。 Looked like these (These is the Path.txt): 看起来像这样(这些是Path.txt):

/home/omarali/scripts/conky/conky-sets/images/record.jpg
/home/omarali/scripts/conky/conky-sets/images/subtle.jpg
/home/omarali/scripts/conky/conky-sets/images/trust.jpg

Then I had a python script to convert the data to arrays and then build files one by one. 然后,我有了一个python脚本,可以将数据转换为数组,然后一个一个地构建文件。 Somehow I got syntax errors and etc. I didn't include the code here since it's too long and I'm considering to start from scratch . 不知何故,我遇到了语法错误等。由于代码太长,我没有在这里包括代码,我正在考虑从头开始

Essentially I would make a new file that has the same name as the image then replace below config variables (WIDTH,HEIGHT,etc) with the images size and path. 本质上,我将创建一个与图像名称相同的新文件,然后将以下配置变量(WIDTH,HEIGHT等)替换为图像的大小和路径。

The template Conky config: 模板Conky配置:

conky.config = {

    --Various settings

    double_buffer = true,                       -- eliminates flicker   

    --Windows

    own_window = true,                          -- create your own window to draw
    own_window_argb_visual = true,              -- use ARGB 
    own_window_type = 'override',               -- keeps the image in the back 


    --Placement

    alignment = 'middle_middle',                -- position of the conky

    --#########################################################################################
    --########################      THE IMPORTANT CHANGABLE STUFF       #######################
    --#########################################################################################

    minimum_width = WIDTH,                      -- minimum height of window
    minimum_height = HEIGHT,                    -- minimum height of window
};

    conky.text = [[
    ${image PATH -s SIZE}

]];
  • WIDTH: width of the image 宽度:图像的宽度
  • HEIGHT: height of the image 高度:图像的高度
  • PATH: path to the image PATH:图像路径
  • SIZE: width + "x" + height of the image 大小:宽度+“ x” +图片高度

This is the final result of the conky config. 这是conky配置的最终结果。

conky.config = {
    minimum_width = 800,                        -- minimum height of window
    minimum_height = 1300,                      -- minimum height of window
};

    conky.text = [[
    ${image /home/omarali/scripts/conky/conky-sets/images/record.jpg -s 800x1300}

]];

So, Just need a simple way to put the images into a conkies that I can later call from a bash script. 因此,只需要一种简单的方法即可将图像放入conkies中,以后可以从bash脚本中调用它。 Python or Bash script will do. Python或Bash脚本即可。

Here what I'm looking for: 我在这里寻找的是:

  1. Build a conky config from each image in a directory. 从目录中的每个图像构建一个conky配置。 let's say dir_pins 假设dir_pins
  2. Each conky should have the same name as the image. 每个conky应与图像具有相同的名称。 (this was my issue) (这是我的问题)
  3. Can be python or bash script, open to ideas but prefer these 2. 可以是python或bash脚本,可以接受想法,但更喜欢这些2。

That's it, as mentioned before I already have a script to auto run my configs. 就是这样,如前所述,我已经有了一个脚本来自动运行配置。 Really appreciate your help. 非常感谢您的帮助。 And open to new ideas. 并接受新想法。

In Python you can read data from all files 在Python中,您可以从所有文件中读取数据

all_widths = open('width.txt').read().splitlines()
all_heights = open('width.txt').read().splitlines()
all_pathes = open('path.txt').read().splitlines()

and use zip() to group width with path 并使用zip()widthpath分组

for width, height, path in zip(all_widths, all_height, all_pathes):
    print(width, height, path)
    # generate conky text

and then you can use text with all paragrath and put {} in places in which you want to put data to generate new text. 然后可以将文本与所有参数结合使用,并将{}放在要放置数据以生成新文本的位置。 Because it use {} to recognize places for values so it need {{ and }} for normal { , } 因为它使用{}来识别值的位置,所以它需要{{}}来作为普通{}

template = '''conky.config = {{
    minimum_width = {WIDTH},                        -- minimum height of window
    minimum_height = {HEIGHT},                      -- minimum height of window
}};

    conky.text = [[
    ${{image {PATH} -s {WIDTH}x{HEIGHT}}}

]];'''
for width, height, path in zip(all_widths, all_height, all_pathes):
    new_text = template.format(WIDTH=width, HEIGHT=height, PATH=path)
    print(new_text)

Now you have to use image filename to create name for conky file (I don't know what extension it uses so I use .txt ) 现在,您必须使用图像文件名为conky文件创建名称(我不知道它使用的扩展名是什么,所以我使用.txt

new_name = path.replace('.jpg', '.txt')

or only last part of path 或仅路径的最后一部分

new_name = path.split('/')[-1].replace('.jpg', '.txt')

and then you can save text 然后您可以保存文本

f = open(new_name, 'w')
f.write(new_text)
f.close()

Full code but not tested: 完整代码,但未经测试:

template = '''conky.config = {{
    minimum_width = {WIDTH},                        -- minimum height of window
    minimum_height = {HEIGHT},                      -- minimum height of window
}};

    conky.text = [[
    ${{image {PATH} -s {WIDTH}x{HEIGHT}}}

]];'''

all_widths = open('width.txt').read().splitlines()
all_heights = open('width.txt').read().splitlines()
all_pathes = open('path.txt').read().splitlines()

for width, height, path in zip(all_widths, all_height, all_pathes):
    new_text = template.format(WIDTH=width, HEIGHT=height, PATH=path)
    #new_name = path.replace('.jpg', '.txt')
    new_name = path.split('/')[-1].replace('.jpg', '.txt')
    f = open(new_name, 'w')
    f.write(new_text)
    f.close()

By the way: you could use os.listdir() or os.walk() to get pathes for images directly from disk. 顺便说一句:您可以使用os.listdir()os.walk()直接从磁盘获取图像的路径。 And module PIL to get sizes of images. 和模块PIL以获取图像的大小。

template = '''conky.config = {{
    minimum_width = {WIDTH},                        -- minimum height of window
    minimum_height = {HEIGHT},                      -- minimum height of window
}};

    conky.text = [[
    ${{image {PATH} -s {WIDTH}x{HEIGHT}}}

]];'''

from PIL import Image

directory = '/home/omarali/scripts/conky/conky-sets/images/'

for filename in os.listdir(directory):
    path = os.path.join(directory, filename)

    width, height = Image.open(path).size

    new_text = template.format(WIDTH=width, HEIGHT=height, PATH=path)
    new_name = filename.replace('.jpg', '.txt')

    f = open(new_name, 'w')
    f.write(new_text)
    f.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM