In my previous blog, I talked about how to use ImageMagick to square a file. Now, in this blog I’m taking it one step further.

I have a directory of photos from all over the internet, and I want to post them to a social media site. If the pictures are taller than 510 pixels, I need to square them as discussed in the last blog (and I’m adding a white padding/background). This will keep them from being cut off when I post them to the specific social media site.

Secondly, if the picture is “wide” vs “tall”, then I don’t need to add the background, but I might as well resize it to save upload/posting time. Some pictures downloaded from the internet are very large, so I’m standardizing on resizing them to a width or height of 510, whichever is smaller.

So in the Powershell, here’s what is happening:
1) Get all the files in a directory ($inputPath) and loop through them one at a time
2) Call ImageMagick “identify” to return the size of the file (primarily I need to know it’s width and height)
3) Use RegEx pattern matching to pull the width/height out of the returned string, convert them to numbers, then use compare them
4) If it is a wide picture, call the ImageMagick resize function
5) If it is a tall picture, call the ImageMagick convert function. Technically, I’m converting to a thumbnail, but it’s a very large thumbnail.

For both #4 and #5 in the above steps, the output file is a variable written to the $targetPath directory with the same filename.

cls

$inputPath = "e:\Photos\ToResize\"
$targetPath = "e:\Photos\Resized\"
$files = get-ChildItem $inputPath  


foreach ($file in $files) 
{

    #$output = &  magick identify e:\Photos\ToResize\022cca60-a2a9-49b8-964e-b228acf517f3.jpg
    # shell out to run the ImageMagick "identify" command 
    $output = &  magick identify $file.FullName 
    $outFilename = $targetPath + $file.Name 

    # sample value of $output: 
    #e:\Photos\ToResize\022cca60-a2a9-49b8-964e-b228acf517f3.jpg JPEG 768x512 768x512+0+0 8-bit sRGB 111420B 0.000u 0:00.000

    write-host $output 

    $pattern = ".* (\d*?)x(\d*?) .*"
    #the first parenthese is for capturing the cityState into the $Matches array
    #the second parentheses are needed above to look for $ (which is end of line)
    #or zip code following the city/state
    $isMatch = $output -match $pattern
    if ($Matches.Count -gt 0)
      {
        #$width = $Matches[1];
        #$height = $Matches[2];

        [int]$width = [convert]::ToInt32($Matches[1]); 
        [int]$height = [convert]::ToInt32($Matches[2]); 

        
        if ($height -gt $width) 
        {
           $orientation = "tall"; 
           # shell out to run the ImageMagick "convert" command 
           & convert -define jpeg:size=510x510 $file.FullName -thumbnail "510x510>" -background white -gravity center -extent "510x510" $outFilename 
        }
        else 
        {
           $orientation = "wide"; 
           # shell out to run the ImageMagick "convert" command 
           & convert -resize 510x510^ $file.FullName $outFilename 
        }
        Write-Host ("file=$($file.name) width=$width height=$height orientation=$orientation `n`n"); 
      }
    else 
      {
       Write-Host ("No matches"); 
      }
}

It took me a while to find a good utility to square images and make them a certain size. The results come from this confusing documentation page for Image Magic.


convert -define jpeg:size=510x510 Test1.jpg -thumbnail "510x510>" -background white -gravity center -extent "510x510" testResult.jpg

Of course you change the sizes and the background color. The page above also show you how to crop the picture if desired to fit the square image (rather than padding with the background color).

The following command (mogrify) does the exact same for every file in the current directory, and outputs to another directory specified by the -path parameter. (reference: mogrify)

mogrify -define jpeg:size=510x510 -thumbnail "510x510>" -background white -gravity center -extent "510x510" -path ../ImageOutputDir *.jpg

Note: When installing on Windows, be sure to check the box to the left of “Install legacy utilities” so it will install the convert.exe program.

See also: Photo.StackExchange/questions/27137K

See also: Downloads for ImageMagick.

 

I made this quick summary of some of the size and limitations and specs for developing Amazon Alexa Skill. Please contact me at Amazon Skill Developer (Dallas, Texas) if you need one developed.

Max 8000 characters of speech when Alexa is speaking based on text.

MP3 – must be bit rate 48kbps, sample rate 16000 Hz, codec version (MPEG Version 2)
https://developer.amazon.com/docs/custom-skills/speech-synthesis-markup-language-ssml-reference.html#h3_converting_mp3

Maximum length of simple “conversational/speech” audio files is 90 seconds.
You can do long audio files of any length, such as podcasts, but that requires a separate programming model.

You can optionally include “cards” which a person can see at:
https://alexa.amazon.com/spa/index.html#cards

Brief explanation of cards here: https://developer.amazon.com/docs/custom-skills/include-a-card-in-your-skills-response.html

In addition to signing up with AWS, you also have to have a login here:
https://developer.amazon.com/edw/home.html#/.
There are two parts to the skill, the skill/vocabulary definition, and the software code.

Logo Images:
You need a small 108×108 PNG and a large 512×512 PNG file for getting certified and putting the skill in the skill store.

If you decide to use “cards”, see above, they have images as well, but the size requirements are different:

small 720w x 480h
large 1200x x 800h