Page 1 of 1

Integrating NConvert with Powershell

Posted: Tue Dec 13, 2016 7:49 pm
by benchmarkjoshw
Hello, I'm looking to create a Powershell script that will allow me to process images based on their file size: any image that comes in over 1 MB gets processed, the rest get ignored. Unfortunately I'm having a bit of trouble integrating NConvert with Powershell, and I'm honestly not sure if this is due to my relative lack of Powershell knowledge or if there really is some issue with NConvert and Powershell (and as such if this is the right place to be asking).

My code is as follows:

Code: Select all

$Dir = "C:\Users\Me\Desktop\Test folder"
$SizeMax = 1000 #KB

# Resize files larger than the $SizeMax
Get-ChildItem -Path $Dir -Recurse | Where { $_.Length / 1000KB -ge $SizeMax } | nconvert -rflag decr -resize 50% 50% -overwrite "C:\Users\Me\Desktop\Test folder\*.jpg"
This is the only way I could get it to work, as adding the $Dir at the end of the NConvert segment caused it to error out. However, running this code doesn't modify based on the file size, it just processes everything in the folder.

Any help would be appreciated, and I do apologize if this is the wrong place to be asking. Thank you!

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 12:20 pm
by xnview
and without -rflag decr?

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 2:09 pm
by benchmarkjoshw
xnview wrote:and without -rflag decr?
Same deal, it applies to all the files in the folder.

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 3:30 pm
by xnview
you use

Code: Select all

"C:\Users\Me\Desktop\Test folder\*.jpg"
for nconvert arguments, so all files

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 3:33 pm
by benchmarkjoshw
xnview wrote:you use

Code: Select all

"C:\Users\Me\Desktop\Test folder\*.jpg"
for nconvert arguments, so all files
Well, without that it doesn't do anything to ANY of the files in the folder. I tried using the $Dir in place of the path, but it errored out.

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 5:22 pm
by cday
I would suggest you first test your NConvert code in isolation and get that running, if you haven't already done so, and then test it with your PowerShell code.

And as you need to use a wildcard in the NConvert input term, I would suggest testing initially with no spaces in the input term path, and also without quotes, to avoid any possible conflict with wildcards: my recollection from past experience is that quotes may not be used with a wildcard when there are spaces in the input term path, although I think spaces in the input filename itself are permissible.

Another possible point to consider is that when NConvert code is run in a batch file the '%' character must be doubled to '%%' to escape it: if the same consideration applies to NConvert code run in PoweShell, your resize term would need to be modified.

Finally, if you have NConvert code that is confirmed as good and still can't obtain the result you need, you will probably need to seek assistance with the PowerShell code elsewhere...

Re: Integrating NConvert with Powershell

Posted: Wed Dec 14, 2016 9:01 pm
by benchmarkjoshw
Well, the issue was with Powershell, not with NConvert. Myself and one of the guys I work with managed to cobble together a working script (which is below if anyone wants to use it for the same reason).

Code: Select all

$Dir = "C:\Users\Me\Desktop\Test_Folder\"

ForEach ($File in (Get-ChildItem $Dir -Recurse -Include *.jpg))
   {

            If ($File.length -gt 1MB)
          {
                        nconvert -resize 50% 50% -overwrite $File
              }  
   }
Thank you all for your help, this was a very enlightening experience.

Re: Integrating NConvert with Powershell

Posted: Thu Dec 15, 2016 2:19 pm
by benchmarkjoshw
I hate to be a bother once more but I had one other question: is it possible to lower the quality of an outputted file with NConvert? I know XnConvert lets you set the quality in the output tab but I'm not seeing anything to do something similar with NConvert.

Re: Integrating NConvert with Powershell

Posted: Thu Dec 15, 2016 2:44 pm
by cday
benchmarkjoshw wrote:I hate to be a bother once more but I had one other question: is it possible to lower the quality of an outputted file with NConvert? I know XnConvert lets you set the quality in the output tab but I'm not seeing anything to do something similar with NConvert.
If you're outputting to a JPEG there is a -q term you can use to set the quality, if you could check the Help file as I'm out of the house at the moment; you could try this revised line in your code:

Code: Select all

nconvert -resize 50% 50% -o jpeg -q 80 -overwrite $File 
If the inserted code is valid, the quality will be set to Q = 80 in place of the default value of 100.

Re: Integrating NConvert with Powershell

Posted: Thu Dec 15, 2016 3:00 pm
by benchmarkjoshw
cday wrote:
benchmarkjoshw wrote:I hate to be a bother once more but I had one other question: is it possible to lower the quality of an outputted file with NConvert? I know XnConvert lets you set the quality in the output tab but I'm not seeing anything to do something similar with NConvert.
If you're outputting to a JPEG there is a -q term you can use to set the quality, if you could check the Help file as I'm out of the house at the moment; you could try this revised line in your code:

Code: Select all

nconvert -resize 50% 50% -o jpeg -q 80 -overwrite $File 
If the inserted code is valid, the quality will be set to Q = 80 in place of the default value of 100.
That worked beautifully. Thank you very much!

Re: Integrating NConvert with Powershell

Posted: Thu Dec 15, 2016 8:15 pm
by cday
benchmarkjoshw wrote:That worked beautifully. Thank you very much!
Please note that checking the NConvert help file on my return home, the -o term above should actually be -out :

Code: Select all

-out format       : Output format name

...

-q value          : JPEG/FPX/WIC/PDF quality
... and the -o term actually defines options for the file name:

Code: Select all

-o filename       : Output filename
              Use # to specify position of numeric enumerator
              Use % to specify source filename
              Use $ to specify full source pathname
              Use $$ to specify source folder name
              Use $EXIF:DateModified[date format] to specify EXIF date modified
              Use $EXIF:DateTaken[date format] to specify EXIF date taken
	      Date format: Please check documentation of strftime
Anyway, it somehow solved the immediate issue even so... :D