SOLVED Processing pairs of images

Ian

Administrator
Joined
Oct 27, 2013
Messages
1,735
Reaction score
630
I'm trying to write a script which compares pairs of MRI images in a 3rd party image processing app. However, I need to pass it some command-line parameters to generate the output images.

I've got a directory full of files of this format:

T1_00001.jpg and T2_00001.jpg, where the 2nd half number sequence increases by 1 and there are always pairs and always start at 00001.

I'm trying to create a script that reads all of the T1_..... files in the directory, counts them and then repeatedly runs a command line script to generate the images using a series of wildcard numbers.

I start off by counting all those T1 files like this:

Code:
Get-ChildItem -filter 'T1_*' -path 'C:\MRIs' | Measure-Object | Select -ExpandProperty Count

That works great and counts all of the files beginning with "T1_" as expected, allowing me to generate the sequence of numbers I need to use.

However, I struggle at the next step (which should be simple!). I need to run a command of this format:

Code:
convert T1_00001.jpg T2_00001.jpg -compose minus -composite Result_00001.jpg

I'm trying to figure out a powershell way of running that command the correct number of times, substituting the numeric part with a 5 digit number generated from the count I collected above. This is where I'm currently stuck, so any help with this would be very much appreciated.
 

Ian

Administrator
Joined
Oct 27, 2013
Messages
1,735
Reaction score
630
Here's where I'm at so far, I think I'm almost there as it outputs the correct text - however it just writes it, rather than executing it as expected:

Code:
$convertFile = "C:\Program Files\ImageMagick-7.0.7-Q16\convert.exe"
$path = "C:\MRIs";
Set-Location -Path $path;
$fileNumber = Get-ChildItem -filter 'T1_*' -path $path | Measure-Object | Select -ExpandProperty Count;

1..$fileNumber | % { 'invoke-expression -command "convert T1_{0:00000}.jpg T2_{0:00000}.jpg -compose minus -composite Result_{0:00000}.jpg"' -f $_ }

Any ideas as to why it isn't executing the commands? I'm sure I'm doing something silly here!
 

Ian

Administrator
Joined
Oct 27, 2013
Messages
1,735
Reaction score
630
All sorted now :D. I had some good advice about taking a different route, they suggested a method in which the files are paired and then the suffix is split off. A much more robust way of doing it:

Code:
$convertFile = "C:\Program Files\ImageMagick-7.0.7-Q16\convert.exe"
$path = "C:\MRIs";
Set-Location -Path $path;

#Find Tx_something.jpg-files
Get-ChildItem -Filter "T?_*.jpg" |

#Group on ID after _ in filename
Group-Object { $_.BaseName.Split("_")[1] } |
ForEach-Object {
    #Find file in group starting with T1
    $input1 = $_.Group -match '^T1'
    $input2 = $_.Group -match '^T2'
    $output = "$($_.Name).jpg"

    convert $($input1.Name) $($input2.Name) -compose minus -composite $output
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top