Quantcast
Channel: OS|DC
Viewing all articles
Browse latest Browse all 64

Powershell: Bulk convert and resize images with a script and how it fits in the deployment space

$
0
0

woolly_by_mars_hill_dbcid6o-pre

Ever wanted to bulk convert your BMP/GIF/ICO/EXIF/JPG/RAW/PNG/TIF images to standard JPG or PNG? Ever needed to resize them while the conversion occurs? You are in the right place!

During the last few days, I’ve been working on adding some option to the excellent script of David Anson who made most of the hard work presented here, (I basically just added the resizing capability). The result is quite interesting and can be downloaded from here.

Why this script is different from those you may already have seen? It relies on a Universal Windows platform API as opposed to .Net API usually used for this kind of job. This API support all the format listed above as input and output and support simple operations on images like rotation, cropping or scaling. Putted simply, it’s the swiss army knife of the image management at your fingers, the perfect API for lazy scripter!


How to use

Let’s say you need to convert a folder of various file format images to PNG (Default option saves to JPG), launch the script like this: 

Get-ChildItem C:\Pictures | .\ImgConvert.ps1 –png

If you also want to scale every image up to 15%, use the -scale parameter:

Get-ChildItem C:\Pictures | .\ImgConvert.ps1 –png –scale 15

ok, very nice, but let’s imagine that you need to scale all your images to the size of your deskop resolution (1920x1080), scale by percent can’t be used, and aspect ractio must be kept (yes no one want to see wife and child’s overweighed!!), here is the command you need:

Get-ChildItem C:\Pictures | .\ImgConvert.ps1 –png –ScaleXto 1920 –ScaleYto 1080

Important note: as resizing is always done with kept aspect ratio, the result will most likely never match the resolution you entered for x and y axis. One axis may always match but both…almost never. This are just physical rules you’ve already taken for granted in real life, so no worry, this will probably not be what you’d except, but smile… it will always be better !

If for some reason you must break those rules, you can add the –NoRatio parameter. Result may not always be as good unless you clearly understand what you are doing.

The -ScaleXto and -ScaleYto can be used separately, if all your images are already in 4/3 ratio but need to be scaled to your desktop resolution, you can use the command like this:

Get-ChildItem C:\Pictures | .\ImgConvert.ps1 –png –ScaleXto 1920

As default option, aspect ratio will be kept, unless you add the -NoRatio switch.

Last goodies, if you need to save the converted images to another directory, there is a –Folder parameter that you can use like this:

Get-ChildItem C:\Pictures | .\ImgConvert.ps1 –png –ScaleXto 256 –ScaleYto 256 –KeepRatio –Folder ‘c:\ResizedImages’

There is also a –FixExtension parameter that works the same as the -FixExtensionIfJpeg from the original script.

Now that we have covered the script usage, let’s ask the big question:



How the heck can this help in the deployment space ???

If you are a regular reader of this blog, you may probably say to yourself: “Nice tool, but hey, I’m deploying computers, I have no use for this!”

Well, maybe not… I had one goal when updating this script: images management automation during a deployment.

For example, I’ve always wanted a no brainer solution to integrate images given by customers like desktop wallpaper or default avatar image. Most of the time, I only get one image that I must edit and scale manually before moving on to the integration.

Now with that tiny script, no matter the format or the size, I can easily produce 1920x1080 background for laptop X and 1600x800 for laptop Y on the fly.

Even nicer, I can also change the default avatar account with a few lines of Powershell:

$NewImage = "C:\Users\Diagg\OneDrive\Images\abstract\cortexlab_by_krypt06-d68hvtk.jpg"
Foreach ($item in (Get-ChildItem "$env:ProgramData\Microsoft\User Account Pictures\*" -Include *.png))
     {
         If (($SubItem = ($item.basename).split("-")).count -gt 1){$Scale = [int]$SubItem[1]}Else{$Scale = 448}
         Remove-item $($item.DirectoryName + "\" + $item.Name + ".bak") -ErrorAction SilentlyContinue
         Rename-Item -Path $item.fullName  -NewName  $($item.Name + ".bak")
         $Newpath = $($item.DirectoryName + "\" + $item.BaseName + "." + $NewImage.Split(".")[1])
         Copy-Item -Path $NewImage -Destination $Newpath
         & "$(split-path $MyInvocation.MyCommand.Path)\ImgConvert.ps1" -files $Newpath -png -ScaleXto $Scale -ScaleYto $Scale
         Remove-Item $Newpath
     }

The script takes care of backing up your previous avatar, and to copy and size the new one where needed:

image

The result can be seen by locking Windows session:

bqj4AebXVu

There are also other cases that you can leverage like dynamic start menu, lock screen or OEM support image etc... The world is yours!!!


Viewing all articles
Browse latest Browse all 64

Trending Articles