Created
November 28, 2024 07:28
-
-
Save Kenya-West/18cb1534432ddb3797bc0ecbae620379 to your computer and use it in GitHub Desktop.
Split long vertical image by sections and join them side-by-side (horisontally) via Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires ImageMagick to be installed: winget install -e --id ImageMagick.ImageMagick --source winget --accept-source-agreements --accept-package-agreements | |
# Ask the user for the input file path | |
$imagePath = Read-Host "Enter the path to the image file" | |
# Ask the user for the number of horizontal splits | |
$numParts = [int](Read-Host "Enter the number of horizontal parts to split the image into") | |
# Validate inputs | |
if (!(Test-Path $imagePath)) { | |
Write-Host "The file does not exist. Exiting." -ForegroundColor Red | |
exit | |
} | |
if ($numParts -lt 1) { | |
Write-Host "Invalid number of parts. Must be at least 1. Exiting." -ForegroundColor Red | |
exit | |
} | |
# Output directory for split parts | |
$outputDir = "$($PWD)\split_parts" | |
if (!(Test-Path $outputDir)) { | |
New-Item -ItemType Directory -Path $outputDir | Out-Null | |
} | |
# Split the image into horizontal parts | |
Write-Host "Splitting the image into $numParts horizontal parts..." | |
magick $imagePath -crop x${numParts}@ +repage "$outputDir\part_%d.png" | |
# Check if parts were created | |
$splitParts = Get-ChildItem -Path $outputDir -Filter "part_*.png" | |
if ($splitParts.Count -eq 0) { | |
Write-Host "Failed to split the image. Exiting." -ForegroundColor Red | |
exit | |
} | |
Write-Host "Split complete. Joining parts horizontally..." | |
# Debugging: List the split parts | |
Write-Host "Split parts:" | |
$splitParts | ForEach-Object { Write-Host $_.FullName } | |
# Correctly format file paths for magick command | |
$partPaths = ($splitParts | ForEach-Object { "`"$($_.FullName)`"" }) -join " " | |
# Debugging: Show the formatted paths | |
Write-Host "Formatted paths for magick command: $partPaths" | |
# Join the split parts horizontally using montage | |
$joinedImagePath = "$($PWD)\joined_image.png" | |
$magickCommand = "magick montage `"$outputDir\part_[0-$($numParts-1)].png`" -tile ${numParts}x1 -geometry +0+0 `"$joinedImagePath`"" | |
# Debugging: Show the full magick command | |
Write-Host "Magick command: $magickCommand" | |
# Execute the magick command using Start-Process | |
Start-Process -FilePath "cmd.exe" -ArgumentList "/c $magickCommand" -Wait -NoNewWindow | |
# Check if the final image was created | |
if (Test-Path $joinedImagePath) { | |
Write-Host "Image parts joined successfully! Output file: $joinedImagePath" -ForegroundColor Green | |
} else { | |
Write-Host "Failed to join the image parts. Exiting." -ForegroundColor Red | |
} | |
# Check existence of the split parts directory and remove it | |
if (Test-Path $outputDir) { | |
Remove-Item $outputDir -Recurse -Force | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment