Trying to prove that Skynet should be running on PowerShell!

Monthly Archives: August 2016

I’ve always enjoyed using PowerShell for things outside of the usual “automating sysadmin tasks” scope, and some time ago I thought it would be cool to use it to create some “branded” wallpapers for my desktop.

Maybe you have a huge number of wallpapers that you have collected over the years, but you’d like to place the PowerShell logo on them to make them sooo much cooler?

Or maybe you just have a bunch of pictures you’d like to watermark to show their origin?

Or maybe you just like to know a few things that you can do with the System.Drawing-namespace and PowerShell.

If any of those are true for you, you might want to try out a function I’ve created called Add-Logo! 🙂

I mostly created it because it was fun to see if it was possible, and it fulfilled it’s purpose a long time ago so I never got to iron out all the bugs (which probably are there), but I thought it could be fun to share it anyway.

(I should probably add that there are some obvious copyright-related things you need consider before doing this depending on what images you pick and how you want to use the results)

What does it do then?
Well, at first it just placed one picture on another picture. I thought this was pretty neat since I could use Get-ChildItem to pipe picture files to it and place the logo on all of them pretty fast, but there was a problem…

I soon realized that every background picture was so different that the logo didn’t look good at the same place on all of them. I started out placing it in the bottom right corner, but some pictures had “too much going on” so the logo was hard to notice, or the contrast was to low (especially text based logos). To solve this I added a parameter for “logo placement”, so it was possible to pick any of the four corners on the background for the logo. I piped the picture files to the function four times (one for each corner) and got a huge collection of “branded wallpapers” where usually at least one out of four looked OK. On top if this, there was an issue with different picture resolutions, on some pictures the logo was huge, and on some it was really tiny.

This was obviously not going to be good enough, so I added some code that would do the following steps:

Sample the pixels of the logo to check what color span it has, the size etc.

Sample the pixels of the background where the logo could be placed

Do some “analysis” of this and place the logo as good as possible

Add parameters for controlling the position of the logo, minimum contrast, relative logo size etc. to the function to make it more dynamic

I’ve had close to zero experience with image processing so this is obviously not anywhere near a professional grade software for doing this kind of thing, but being able to use PowerShell for working with pictures is still pretty cool IMHO 🙂

Some usage example follows:

First begin with a picture that can act as a logo, I took the liberty of using the PowerShell logo (registered trademark of Microsoft):

and this picture of a few clouds (should go hand in hand 🙂 ):

I personally think that logotypes usually look better where the background is “solid”, more so than where the contrast is the greatest, so the default settings of the parameters is to have a preference towards that (as long as the minimum allowed contrast level is achieved).

Since the contrast value is met (controlled by the “MinimumContrast” parameter) and the color is pretty solid it ended up in the top left corner.

Let’s do another one, using the same PowerShell logo but on this picture:

The command is now run with Verbose output which will show you a bit more information about why the function picked a certain corner:

Results:

This might seem a bit odd at first. The top left corner seems to be a better placement for the logo, and it’s contrast is certainly greater as you can see from the verbose output, but the problem here is that the logo doesn’t really fit in the black area and therefore the function sampled some of the “blue/earth pixels” as well since that’s where parts of the logo would have ended up.

Let’s try running the same command again, but this time with the “ProportionFactor” parameter, which enables scaling of the logotype (higher value makes the logo smaller):

Results:

Now the top left corner had both the greatest contrast and the most solid color since the logo actually fits in the black area!

As you can see, the function usually needs some tuning from the standard values depending on the logo you are using (play around with MinimumContrast and MaxColorSpan!), but once you’re happy with a few test pictures, you can usually just pipe all the images you want to it and you’ll have a bunch of “branded” desktop wallpapers to use!

The function also includes other parameters, for example how close to the edge you want to place the logo or if you just want to place it in the same corner on all of the pictures.

Summary
While the task of manipulating images can be solved in a lot of other (better/more efficient) ways, I think it’s still pretty cool that you can use PowerShell to achieve something like this. So while the code itself in this function isn’t really something to write home about, the fact that PowerShell can be used for such a variety of tasks kind of is!