PowerShell/Scripts/convert-image2pixelated-frames.ps1

52 lines
1.8 KiB
PowerShell
Raw Normal View History

2023-02-16 13:24:32 +01:00
<#
.SYNOPSIS
Converts an image into pixelated frames
2023-02-16 13:24:32 +01:00
.DESCRIPTION
This PowerShell script converts a single image file into a series of pixelated frames in a target dir.
2023-02-26 21:09:46 +01:00
Requires ImageMagick 6.
2023-02-16 13:24:32 +01:00
.PARAMETER SourceFile
Specifies the path to the image source file
.PARAMTER TargetDir
Specifies the path to the target folder
.EXAMPLE
PS> ./convert-image2pixelated-frames C:\my_photo.jpg C:\Temp
2023-02-16 13:24:32 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-03-06 21:29:00 +01:00
param([string]$SourceFile = "", [string]$TargetDir = "", [int]$Frames = 700)
2023-02-16 13:24:32 +01:00
try {
if ($SourceFile -eq "") { $SourceFile = Read-Host "Enter file path to source image file" }
if ($TargetDir -eq "") { $TargetDir = Read-Host "Enter file path to target directory" }
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-03-06 21:29:00 +01:00
"⏳ (1/$Frames) Checking source image file..."
2023-02-26 21:09:46 +01:00
if (!(Test-Path "$SourceFile" -pathType leaf)) { throw "Can't access source image file: $SourceFile" }
2023-02-16 13:24:32 +01:00
$Basename = (Get-Item "$SourceFile").Basename
2023-02-26 21:09:46 +01:00
2023-03-06 21:29:00 +01:00
"⏳ (2/$Frames) Searching for ImageMagick 6 executable..."
2023-02-26 21:09:46 +01:00
& convert-im6 --version
if ($lastExitCode -ne "0") { throw "Can't execute 'convert-im6' - make sure ImageMagick 6 is installed and available" }
2023-02-16 13:24:32 +01:00
$Factor = 0.001
2023-03-06 21:29:00 +01:00
for ($i = 0; $i -lt $Frames; $i++) {
$FrameNo = '{0:d4}' -f $i
$TargetFile = "$TargetDir/frame_$($FrameNo).jpg"
2023-03-06 21:29:00 +01:00
"⏳ ($($i + 3)/$Frames) Converting with pixelation factor $Factor to $TargetFile..."
2023-02-16 13:24:32 +01:00
$Coeff1 = 100.0 * $Factor
$Coeff2 = 100.0 / $Factor
2023-02-26 21:09:46 +01:00
& convert-im6 -scale $Coeff1% -scale $Coeff2% "$SourceFile" "$TargetFile"
2023-02-16 13:24:32 +01:00
$Factor += 0.0005
}
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-03-06 21:29:00 +01:00
"✅ converted image $SourceFile to $Frames pixelated frames in 📂$TargetDir in $Elapsed sec."
2023-02-16 13:24:32 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}