Integrating NConvert with Powershell

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, xnview

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Integrating NConvert with Powershell

Postby benchmarkjoshw » Tue Dec 13, 2016 7:49 pm

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!

User avatar
xnview
Author of XnView
Posts: 27088
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Integrating NConvert with Powershell

Postby xnview » Wed Dec 14, 2016 12:20 pm

and without -rflag decr?
Pierre.

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Re: Integrating NConvert with Powershell

Postby benchmarkjoshw » Wed Dec 14, 2016 2:09 pm

xnview wrote:and without -rflag decr?


Same deal, it applies to all the files in the folder.

User avatar
xnview
Author of XnView
Posts: 27088
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Integrating NConvert with Powershell

Postby xnview » Wed Dec 14, 2016 3:30 pm

you use

Code: Select all

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

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Re: Integrating NConvert with Powershell

Postby benchmarkjoshw » Wed Dec 14, 2016 3:33 pm

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.

cday
XnThusiast
Posts: 1406
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Integrating NConvert with Powershell

Postby cday » Wed Dec 14, 2016 5:22 pm

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...

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Re: Integrating NConvert with Powershell

Postby benchmarkjoshw » Wed Dec 14, 2016 9:01 pm

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.

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Re: Integrating NConvert with Powershell

Postby benchmarkjoshw » Thu Dec 15, 2016 2:19 pm

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.

cday
XnThusiast
Posts: 1406
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Integrating NConvert with Powershell

Postby cday » Thu Dec 15, 2016 2:44 pm

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.

benchmarkjoshw
Posts: 6
Joined: Tue Dec 13, 2016 7:40 pm

Re: Integrating NConvert with Powershell

Postby benchmarkjoshw » Thu Dec 15, 2016 3:00 pm

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!

cday
XnThusiast
Posts: 1406
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Integrating NConvert with Powershell

Postby cday » Thu Dec 15, 2016 8:15 pm

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


Return to “NConvert”

Who is online

Users browsing this forum: No registered users and 2 guests