Page 1 of 1

Using PowerShell to get image size

Posted: January 27th, 2018, 5:18 pm
by jsmorley
In another thread, someone had asked about getting the actual width and height on an image file. While the suggested solution was to use the ImageSize plugin, and that works fine, I wanted to suggest an alternative, in case you don't want to have to use a plugin, and include it in your .rmskin for distribution.

What this does is two things:

1) Use a simple PowerShell .ps1 script to get the image width and height and return them as a string "width|height".

2) Use a simple Lua script to break this string into separate "width" and "height" values, which can be returned to any measure or meter using Inline Lua Variables.
PSImageSize_1.0.rmskin
GIF.gif
PSImageSize.ini (Skin):

Code: Select all

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

[Metadata]
Name=PSImageSize
Author=JSMorley
Information=Demonstrates using PowerShell to get the width and height of an image,|and some simple Lua to break that string into separate elements.
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Version=Jan 27, 2018

[Variables]
ImageName=#@#Images\Lampoon.jpg

[MeasureRun]
Measure=Plugin
Plugin=RunCommand
Program=PowerShell.exe
Parameter=-NoProfile -ExecutionPolicy Bypass -Command "& '.\ImageSize.ps1' '#ImageName#'"
OutputType=ANSI
FinishAction=[!ToggleMeter MeterSize][!UpdateMeter MeterSize][!Redraw]

[Lua]
Measure=Script
ScriptFile=ImageSize.lua
Disabled=1

[MeterImage]
Meter=Image
ImageName=#ImageName#
H=300
PreserveAspectRatio=1
LeftMouseUpAction=[!CommandMeasure MeasureRun "Run"]

[MeterSize]
Meter=String
Y=5R
W=([&MeterImage:W]-10)
X=([&MeterImage:W] / 2)
StringAlign=Center
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=Width: [&Lua:Split('[&MeasureRun]','Width')] | Height: [&Lua:Split('[&MeasureRun]','Height')]
UpdateDivider=-1
Hidden=1
DynamicVariables=1
ImageSize.ps1 (PowerShell):

Code: Select all

$imageFile = $args[0]

add-type -AssemblyName System.Drawing
$image = New-Object System.Drawing.Bitmap $imageFile
$imageWidth = $image.Width
$imageHeight = $image.Height
write-host -NoNewline "$imageWidth|$imageHeight"
ImageSize.lua (Lua):

Code: Select all

function Split(stringArg, dimensionArg)

	if stringArg == '' then return 0 end
	
	sep = '|'
	dimElements = {}
	
	for element in stringArg:gmatch('[^'..sep..']+') do table.insert(dimElements, element) end
	
	if #dimElements ~= 2 then
		print('stringArg of '..stringArg..' is invalid')
		return 0
	end
	
	if string.upper(dimensionArg) == 'WIDTH' then
		return dimElements[1]
	elseif string.upper(dimensionArg) == 'HEIGHT' then
		return dimElements[2]
	else
		print('dimensionArg of '..dimensionArg..' is invalid')
		return 0
	end

end
Feel free to ask any questions...