Skip to content

Instantly share code, notes, and snippets.

@kneeprayer
Last active April 5, 2019 07:59
Show Gist options
  • Save kneeprayer/e3c3cf1c89cce6eea2b9462d0c5d2b3b to your computer and use it in GitHub Desktop.
Save kneeprayer/e3c3cf1c89cce6eea2b9462d0c5d2b3b to your computer and use it in GitHub Desktop.
Merge two Images in same folder
#
# Usage : .\Merge-Image.ps1 .\test*.png .\mergeImages\
#
$fileList = Get-ChildItem $args[0]
for ($i = 0; $i -lt $fileList.count; $i = $i + 2) {
# Add System.Drawing assembly
Add-Type -AssemblyName System.Drawing
$firstImage = [System.Drawing.Image]::FromFile((Get-Item $fileList[$i]))
if (($i + 1) -lt $fileList.count) {
$secondImage = [System.Drawing.Image]::FromFile((Get-Item $fileList[$i + 1]))
}
else {
# make empty Image
$secondImage = New-Object System.Drawing.Bitmap($firstImage.width, $firstImage.height)
}
[Int]$new_width = [Int]$firstImage.Width
[Int]$new_height = [Int]$($firstImage.height + $secondImage.height)
# Create empty canvas for the new image
$newImage = New-Object System.Drawing.Bitmap($new_width, $new_height)
# Draw new image on the empty canvas
$graph = [System.Drawing.Graphics]::FromImage($newImage)
$graph.DrawImage($secondImage, 0, [Int]$firstImage.height, [Int]$secondImage.Width, [Int]$secondImage.height)
$graph.DrawImage($firstImage, 0, 0, [Int]$firstImage.Width, [Int]$firstImage.height)
# Create window to display the new image
if ($Display) {
Add-Type -AssemblyName System.Windows.Forms
$win = New-Object Windows.Forms.Form
$box = New-Object Windows.Forms.PictureBox
$box.Width = $new_width
$box.Height = $new_height
$box.Image = $newImage
$win.Controls.Add($box)
$win.AutoSize = $true
$win.ShowDialog()
}
$newFileName = Split-Path -Path (get-Item $fileList[$i]).FullName -Leaf
$newFileName = $newFileName -replace ".png", "_marge_$i.png"
$newFileName = "$($(get-Item $args[1]).fullname)\$newFileName"
echo $newFileName
$newImage.Save($newFileName)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment