0

I found very good Pinterest pictures which I want to randomly show on my desktop. I already have a script(s) that randomly call conky configs from a directory. (Then I manually make bash scripts that call from 1 to 3+ conkies at a time)

Tried to do some python scripting but it didn't... I over complicated it. Here's the GIT FILES:

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. Like width.txt, path.txt,etc. Looked like these (These is the 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. 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.

The template Conky config:

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
  • SIZE: width + "x" + height of the image

This is the final result of the conky config.

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. Python or Bash script will do.

Here what I'm looking for:

  1. Build a conky config from each image in a directory. let's say dir_pins
  2. Each conky should have the same name as the image. (this was my issue)
  3. Can be python or bash script, open to ideas but prefer these 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.

  • I don't know why you keep size and path in separated files. You should keep them together for "data integrity". – furas Sep 14 '19 at 19:52
  • Python has string formating like `"image {} -s {}".format(PATH, SIZE)` or in new Python `f-string` `f"image {PATH} -s {SIZE}"` so you can create string with all text, and use `for` loop to generate many versions and save in different files. – furas Sep 14 '19 at 19:54
  • If you think that separated files are simpler then use them. You can read both files and use `zip()` to group `path` with `size` - `for (path, size) in zip(all_pathes, all_sizes):` - and then use it with string formating. – furas Sep 14 '19 at 19:58
  • My real issue was reading and writing the file. As you see the config file is a whole paragraph... I don't know how to make the file name same as the image name and at the same time edit every file with the images' size (width and height). – omarali linux Sep 14 '19 at 20:06
  • image filename is a string so use string functions to get interesting part of name and create string with new filename. And later use `open(new_name, 'w')` and `write()` to save it with new name. – furas Sep 14 '19 at 20:11
  • you can put paragraph as text in code and put `{}` in this text in places in which you want to put values - and string formating will put values in place of `{}` – furas Sep 14 '19 at 20:13

1 Answers1

0

In Python you can read data from all files

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

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)

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. And module PIL to get sizes of images.

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()
furas
  • 134,197
  • 12
  • 106
  • 148