Can .lua be scripted to randomly select 1 image from a dir of several images? [SOLVED]

Add functionality to your desktop
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Can .lua be scripted to randomly select 1 image from a dir of several images? [SOLVED]

Post by Logansfury »

Hello,

I am working on converting a routine done by python3 to a conky version. To do so, I need some .lua help please.

What would be the .lua code to navigate to a dir of images, randomly select one image, and then display it?

Thank you for reading,

Logan
Last edited by Logansfury on Fri Feb 16, 2024 1:58 am, edited 1 time in total.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

Why lua?
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Hey Koentje,

How have you been?

This answer was given on the internet some years ago:

Code: Select all

Conky does have an image object: http://conky.sourceforge.net/variables.html

You would use it similar to: ${image /some/path/to/image.png -p 250,0 -s 80x80}

It is likely your mileage may vary with this one.
Use of this code however results in a washed out and partially transparentized image:

Image

When using Lua, the same picture displays in perfect focus and integrity:

Image
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

I'm no hero in lua... i had already made a random images picker in bash. But i see your problem with the transparency. If i can have the lua script, then i'll fix the bash script to pick a random image from one directory and copy it to another directory were lua loads the image from.
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Koentje wrote: Thu Feb 15, 2024 11:33 am I'm no hero in lua... i had already made a random images picker in bash. But i see your problem with the transparency. If i can have the lua script, then i'll fix the bash script to pick a random image from one directory and copy it to another directory were lua loads the image from.
That would be wonderful! Bleys posted the .lua here publicly for me so I'm sure he won't mind my reposting it. The last 4 digits in the DrawImage command are 1/2 the image width, 1/2 the image height, image width, image height. Why this is necessary I don't know, but using different figures for the half values results in only the bottom-right corner of your image being displayed. Those two values have to be image center to display full picture

conkyrc:

Code: Select all

--[[
Bleys 2024 

]]
conky.config = {
-- — Conky settings

	background = false,
	update_interval = 1,
	total_run_times = 0,
	no_buffers = true,	

-- — Window specifications with Background

	own_window = true,
	own_window_type = dock,
    own_window_hints = 'skip_taskbar,skip_pager,undecorated,above',
    own_window_transparent = false,
	own_window_argb_visual = true,
	own_window_argb_value = 0,
	own_window_colour = '000000', 
	
	double_buffer = true,
	minimum_width = 300, minimum_height = 263,
	alignment = 'middle_middle',
	
	gap_x = 20,
	gap_y = 20,


-- Lua Load
	lua_load = '/home/logansfury/.conky/Biohazzard/pic.lua',
	lua_draw_hook_post = 'main',		
};
conky.text = [[

]];
And the pic.lua:

Code: Select all

--[[
2024 Bleys


]]

require 'cairo'
require "imlib2"
home_path = os.getenv ('HOME')


function fDrawImage(cr,path,xx,yy,ww,hh,arc)
    cairo_save (cr)
    local img =  cairo_image_surface_create_from_png(path)
    local w_img, h_img = cairo_image_surface_get_width(img), cairo_image_surface_get_height(img)
    cairo_translate (cr, xx, yy)
    if arc then
        cairo_rotate (cr, arc)
    end
    cairo_scale (cr, ww/w_img, hh/h_img)
    cairo_set_source_surface (cr, img, -w_img/2, -h_img/2)
    cairo_paint (cr)
    cairo_surface_destroy (img)
    collectgarbage ()
    cairo_restore (cr)
end

function conky_main()
	if conky_window==nil then return end
	local cs=cairo_xlib_surface_create(conky_window.display,conky_window.drawable,conky_window.visual, conky_window.width,conky_window.height)	
	local cr=cairo_create(cs)		
	local updates=conky_parse('${updates}')
	update_num=tonumber(updates)
  
	if update_num>4 then
        fDrawImage(cr,'/home/logansfury/Pictures/bio_sm.png',150,132,300,263)
	end

   cairo_surface_destroy(cs)
   cairo_destroy(cr)
end
Thanks :D
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

Here ya go. Unpack in your conky folder and run it. If you have new images to show, place them in the folder image-in. The folder image-out is were the random image is copied to and has only one file image.png in it!

show-random-image-lua.tar.gz
(53.82 KiB) Downloaded 11 times
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Safari, Sogoodi!

It works, showing a randomizing display of weather icons.

Is it possible to get an edited version of the code? I was looking for one image to be chosen from several, and just that image being displayed until the conky was stopped.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

???

${execpi 60000 ./random-image.sh}
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Aha, I believe I see, make the recurrance happen in hours!
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Koentje wrote: Thu Feb 15, 2024 12:14 pm ???

${execpi 60000 ./random-image.sh}
Brilliant! That has it paused on the first selected image, which will give the bash script that launched the conky all the time it needs to run the audio bit and then kill the conky profile.

It's randomizing image selection on subsequent runs perfectly.

Thank you very much :D
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

I just realized I separated my image and audio files by loading the image-in with the desired pictures only.

Was conky capable of playing an .mp3 file?

If so, would it be a difficult edit of the bash script to have it transfer not only the selected .png, but also the .mp3 in the directory with the same name?

ie, if the directory contains:

pic1.png
pic1.mp3
pic2.png
pic2.mp3
pic3.png
pic3.mp3

etc

Is it possible to have conky play the matching .mp3 for the .png it selects?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

Yes, that is possible..
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Koentje wrote: Thu Feb 15, 2024 12:44 pm Yes, that is possible..
I would be most grateful if you had the time :)
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

Here ya go! Unpack in your conky folder and run.. and install sox!

show-random-image-lua-v2.tar.gz
(26.23 KiB) Downloaded 12 times
Last edited by Koentje on Thu Feb 15, 2024 1:19 pm, edited 1 time in total.
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

I increased the duration from 2 to 60000 to prevent image change, and ran it.

It is still randomizing images perfectly on each run, but I am not hearing my mp3.

do my files need to literally be named "pic1", "pic2" or is it OK to have other pairs of names for the pics and mp3s?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

No, you need to install sox to use the play tool...
Naming doesn't matter, as long as the image file and mp3 are named the same!

pic1.png
pic1.mp3
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Koentje wrote: Thu Feb 15, 2024 1:20 pm No, you need to install sox to use the play tool...
Naming doesn't matter, as long as the image file and mp3 are named the same!

pic1.png
pic1.mp3
I turned out to aleady have sox, but I am not hearing my .mp3s. I have confirmed that they play fine on my various media players.

Code: Select all

logansfury@OptiPlex-5040:~$ sudo apt install sox
[sudo] password for logansfury:           
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
sox is already the newest version (14.4.2+git20190427-2+deb11u2ubuntu0.22.04.1).
sox set to manually installed.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Koentje
Level 7
Level 7
Posts: 1581
Joined: Tue Jan 04, 2022 6:23 pm
Location: Netherlands

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Koentje »

Let me guess... you have spaces in your path or filename??

Instead of the play line, put this in its place:

gst-launch-1.0 playbin uri=file://"$mp3" &>/dev/null
Image
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

Koentje wrote: Thu Feb 15, 2024 1:28 pm Let me guess... you have spaces in your path or filename??

Instead of the play line, put this in its place:

gst-launch-1.0 playbin uri=file://"$mp3" &>/dev/null
I am using your folder as downloaded with the hyphens so no spaces in the path. all the mp3 and png files are simple non spaced names as well.

Trying your alternate command now.
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
User avatar
Logansfury
Level 6
Level 6
Posts: 1237
Joined: Fri Oct 27, 2023 4:08 pm
Location: Las Vegas NV, USA

Re: Can .lua be scripted to randomly select 1 image from a dir of several images?

Post by Logansfury »

You're alternate command worked, the mp3 played :)

The only issue is I wanted the image displayed before the mp3 ran. What just happened was the mp3 read over a blank screen then popped up the image.

Can this be edited please?
Image <-- Cick for sudo inxi --usb -Fxxxnmprz output, updated hourly!
Post Reply

Return to “Compiz, Conky, Docks & Widgets”