It is currently June 25th, 2019, 12:27 am

Copy an image with lua

Discuss the use of Lua in Script measures.
User avatar
balala
Rainmeter Sage
Posts: 8308
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Copy an image with lua

balala » September 3rd, 2014, 6:06 pm

Hi all,
I'm working now on a skin and as a part of it I'd like to copy a jpg image from a folder into another. For this I've created a small lua script, but it works just for txt files (if I copy a txt file, it is all right, but a jpg isn't copied entirely: the copy of a 20 KB image has 300 B and can't be opened).
I'm asking a lua expert to help me to can copy the image. So, here's my code:

Code: Select all

function CopyImg(src, dest)
	local mp_log = io.open(src, "r")
	local output = io.open(dest, "w")
	output:write(mp_log:read("*all"))
	output:close()
	mp_log:close()
end

function Initialize()
	CopyImg("d:/1.jpg", "e:/1.jpg")
end
I think it's obvious that I want to copy the d:/1.jpg file to e:/1.jpg.
Thanks in advance.
User avatar
killall-q
Posts: 285
Joined: August 14th, 2009, 8:04 am

Re: Copy an image with lua

killall-q » September 4th, 2014, 1:57 am

You can use command line copy: >copy D:\1.jpg E:\

Use os.execute:

Code: Select all

os.execute('copy D:\\1.jpg E:\\')
This will flash a command prompt window for a split second. If you don't want that, use RunCommand.
User avatar
balala
Rainmeter Sage
Posts: 8308
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Copy an image with lua

balala » September 4th, 2014, 11:46 am

Thank you killall-q, the os.execute indeed works. It was pretty simple, the solutions I found on the internet were more complicated.
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Copy an image with lua

jsmorley » September 4th, 2014, 1:08 pm

Avoiding the quick "popup" cmd.exe window is worth doing in my view...

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
InFile=#CURRENTPATH#TestInFile.jpg
OutFile=#CURRENTPATH#TestOutFile.jpg

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Parameter=copy "#InFile#" "#OutFile#"
DynamicVariables=1

[MeasureScript]
Measure=Script
ScriptFile=#CURRENTPATH#Test.lua
UpdateDivider=-1

[MeterOne]
Meter=Image
W=50
H=50
SolidColor=255,255,255,255
LeftMouseUpAction=[!CommandMeasure MeasureScript "FileCopy()"]
Lua:

Code: Select all

function Initialize()

end

function Update()

end

function FileCopy()

	SKIN:Bang('!CommandMeasure', 'MeasureRun', 'Run')

end
Not too hard.

You can almost certainly "pass" the file names from the skin to the Lua as well, then use a SKIN:Bang('!SetOption', ..) to set the "Parameter" option on the RunCommandPlugin and then fire it, but I caution that it quickly gets complicated dealing with the reserved "\" character in folder paths in Windows, and proper "quoting" back and forth to deal with spaces in the path names. If you need to change it dynamically, it might be easier in the long run to use !SetVariable directly in the skin to change the file paths and names, then just use the Lua to simply "fire" the RunCommand measure at whatever point in your Lua code is appropriate.
User avatar
balala
Rainmeter Sage
Posts: 8308
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Copy an image with lua

balala » September 4th, 2014, 6:09 pm

That's great, jsmorley. Thanks, this solution is awesome! I'll use this one.
User avatar
Krainz
Posts: 137
Joined: May 27th, 2012, 5:16 am

Re: Copy an image with lua

Krainz » December 4th, 2014, 2:30 pm

Hi, I have been trying with the lua script posted by jsmorley, but it's not working. The image is not being copied.

And yes, it's the exact same code, with Rainmeter 3.2 r2326
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Copy an image with lua

jsmorley » December 4th, 2014, 3:06 pm

Krainz wrote:Hi, I have been trying with the lua script posted by jsmorley, but it's not working. The image is not being copied.

And yes, it's the exact same code, with Rainmeter 3.2 r2326
I assure you it does work. Try this test skin:
FileCopyTest_1.0.rmskin
Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
InFile=#@#TestInFile.jpg
OutFile=#@#TestOutFile.jpg

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Parameter=copy "#InFile#" "#OutFile#"

[MeasureScript]
Measure=Script
ScriptFile=#CURRENTPATH#FileCopyTest.lua
UpdateDivider=-1

[MeterClickMe]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Click Me...
LeftMouseUpAction=[!CommandMeasure MeasureScript "FileCopy()"]

[MeterImageOne]
Meter=Image
X=0r
Y=3R
W=64
PreserveAspectRatio=1
ImageName=#InFile#

[MeterImageTwo]
Meter=Image
X=3R
Y=0r
W=64
PreserveAspectRatio=1
ImageName=#OutFile#
DynamicVariables=1
Lua:

Code: Select all

function Initialize()

end

function Update()

end

function FileCopy()

	SKIN:Bang('!CommandMeasure', 'MeasureRun', 'Run')

end
Before clicking:
1.jpg
After clicking:
2.jpg
test.gif
The DynamicVariables=1 on the meter showing the "new / copied" image file is important. When the skin is first loaded, that image does not exist, and the meter returns a "file not found" error. It won't try again (to not just hammer the log with errors) unless it is dynamically evaluating that #Variable#" on each update.

Really you don't need the Lua, you could directly !CommandMeasure the RunCommand plugin to fire it. This was mostly just an example of how you could do a "file copy" that might be done in Lua, without getting the cmd.exe popup...
You do not have the required permissions to view the files attached to this post.
User avatar
balala
Rainmeter Sage
Posts: 8308
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Copy an image with lua

balala » December 4th, 2014, 3:27 pm

I agree with jsmorley, the skin works indeed. Try to download the skin posted by jsmorley above and try it. It must work!
User avatar
jsmorley
Developer
Posts: 19268
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Copy an image with lua

jsmorley » December 4th, 2014, 3:49 pm

Probably a better option, so you don't get errors in the log at all, is this:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]
InFile=#@#TestInFile.jpg
OutFile=#@#TestOutFile.jpg

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Parameter=copy "#InFile#" "#OutFile#"
FinishAction=[!SetOption MeterImageTwo ImageName "#OutFile#"]
 
[MeasureScript]
Measure=Script
ScriptFile=#CURRENTPATH#FileCopyTest.lua
UpdateDivider=-1

[MeterClickMe]
Meter=String
FontSize=11
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Click Me...
LeftMouseUpAction=[!CommandMeasure MeasureScript "FileCopy()"]

[MeterImageOne]
Meter=Image
X=0r
Y=3R
W=64
PreserveAspectRatio=1
ImageName=#InFile#

[MeterImageTwo]
Meter=Image
X=3R
Y=0r
W=64
PreserveAspectRatio=1
That way the RunCommand measure dynamically "sets" the ImageName on the image meter when it is done.
User avatar
Krainz
Posts: 137
Joined: May 27th, 2012, 5:16 am

Re: Copy an image with lua

Krainz » December 4th, 2014, 3:55 pm

Didn't work. Probably something is wrong on my end. How do I check/fix that?

Tried Running Rainmeter as administrator, still not working.

Log:
WARN (00:14:22.935) FileCopyTest\FileCopyTest.ini - [MeasureRun]: !CommandMeasure: Not supported