코딩 모르지만..결과물이..
파워쉘로 만든 작업물
- 더블클릭 하여 실행하면 해당 폴더에 있는 모든 이미지를 변환
- 드래그앤드랍으로 파일을 넣으면 해당 파일만 변경
- 가로폭이 1600 픽셀보다 크면 1600으로 줄임
- JPG가 아니면 JPG로 변경
- 압축품질 설정 75
# Recommended file encoding: UTF-8 with BOM
try { chcp 65001 | Out-Null } catch {}
[Console]::InputEncoding = [System.Text.Encoding]::UTF8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8
# Optional arguments
# arg0..n : dropped files/folders
# To change defaults, edit the values below
$jpgQuality = 75
$maxWidth = 1600
$outputFolderName = "converted_to_jpg"
function Show-Text($text) {
Write-Host $text
}
function Pause-End {
$null = Read-Host "Press Enter to exit"
}
function Is-SupportedImageFile($path) {
if (-not (Test-Path $path -PathType Leaf)) { return $false }
$ext = [System.IO.Path]::GetExtension($path).ToLower()
return $ext -in @(".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff")
}
function Get-InputFiles($items) {
$result = New-Object System.Collections.ArrayList
foreach ($item in $items) {
if (-not (Test-Path $item)) { continue }
if (Test-Path $item -PathType Container) {
$files = Get-ChildItem -Path $item -Recurse -File | Where-Object {
$_.Extension.ToLower() -in @(".jpg", ".jpeg", ".png", ".webp", ".bmp", ".tif", ".tiff")
}
foreach ($f in $files) {
[void]$result.Add($f.FullName)
}
}
elseif (Test-Path $item -PathType Leaf) {
if (Is-SupportedImageFile $item) {
[void]$result.Add((Resolve-Path $item).Path)
}
}
}
return @($result | Sort-Object -Unique)
}
function Get-OutputBaseFolder($items) {
if ($items.Count -eq 0) {
return (Get-Location).Path
}
$firstItem = $items[0]
if (Test-Path $firstItem -PathType Container) {
return (Resolve-Path $firstItem).Path
}
if (Test-Path $firstItem -PathType Leaf) {
return Split-Path (Resolve-Path $firstItem).Path -Parent
}
return (Get-Location).Path
}
function Format-Bytes($bytes) {
if ($bytes -ge 1GB) { return "{0:N2} GB" -f ($bytes / 1GB) }
elseif ($bytes -ge 1MB) { return "{0:N2} MB" -f ($bytes / 1MB) }
elseif ($bytes -ge 1KB) { return "{0:N2} KB" -f ($bytes / 1KB) }
else { return "$bytes B" }
}
function Get-ReductionPercent($originalBytes, $newBytes) {
if ($originalBytes -le 0) { return 0 }
return [math]::Round((($originalBytes - $newBytes) / $originalBytes) * 100, 2)
}
$inputItems = @($args)
if ($inputItems.Count -eq 0) {
$inputItems = @((Get-Location).Path)
Show-Text "Mode : Current folder"
Show-Text "Input : $(Get-Location).Path"
} else {
Show-Text "Mode : Drag and drop / command-line arguments"
foreach ($item in $inputItems) {
Show-Text "Input : $item"
}
}
$outputBaseFolder = Get-OutputBaseFolder $inputItems
$outputFolder = Join-Path $outputBaseFolder $outputFolderName
$magickCmd = Get-Command magick -ErrorAction SilentlyContinue
if (-not $magickCmd) {
$downloadUrl = "https://imagemagick.org/script/download.php#windows"
Show-Text ""
Show-Text "=========================================="
Show-Text " ImageMagick is not installed."
Show-Text "=========================================="
Show-Text "Please install ImageMagick for Windows from:"
Show-Text $downloadUrl
Show-Text ""
Show-Text "During installation, enable this option:"
Show-Text "- Add application directory to your system path"
Show-Text ""
Show-Text "Opening the download page in your browser..."
try {
Start-Process $downloadUrl
} catch {
Show-Text "Could not open the browser automatically. Please open the URL manually."
}
Pause-End
exit 1
}
$files = Get-InputFiles $inputItems
$total = @($files).Count
Show-Text "Image files found : $total"
Show-Text "Supported input : JPG, JPEG, PNG, WebP, BMP, TIF, TIFF"
Show-Text "Output format : JPG"
Show-Text "JPEG quality : $jpgQuality"
Show-Text "Max width : $maxWidth px"
Show-Text "Output folder : $outputFolder"
if ($total -eq 0) {
Show-Text ""
Show-Text "No supported image files were found."
Pause-End
exit 0
}
if (-not (Test-Path $outputFolder)) {
New-Item -ItemType Directory -Path $outputFolder -Force | Out-Null
}
$count = 0
$successCount = 0
$failCount = 0
$totalOriginalBytes = 0L
$totalNewBytes = 0L
foreach ($inputFile in $files) {
$count++
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($inputFile)
$outputFile = Join-Path $outputFolder ($baseName + ".jpg")
if (Test-Path $outputFile) {
$index = 1
do {
$newName = "{0}_{1}.jpg" -f $baseName, $index
$outputFile = Join-Path $outputFolder $newName
$index++
} while (Test-Path $outputFile)
}
$percent = [int](($count / $total) * 100)
Write-Progress -Activity "Converting images to JPG" -Status "$count / $total" -PercentComplete $percent
$originalBytes = (Get-Item $inputFile).Length
& $magickCmd.Source `
$inputFile `
-auto-orient `
-background white `
-alpha remove `
-alpha off `
-resize "${maxWidth}x>" `
-strip `
-quality $jpgQuality `
$outputFile
if (($LASTEXITCODE -eq 0) -and (Test-Path $outputFile)) {
$newBytes = (Get-Item $outputFile).Length
$reduction = Get-ReductionPercent $originalBytes $newBytes
$totalOriginalBytes += $originalBytes
$totalNewBytes += $newBytes
$successCount++
Show-Text ("OK : {0}" -f $inputFile)
Show-Text (" -> : {0}" -f $outputFile)
Show-Text (" Size: {0} -> {1} ({2}%)" -f (Format-Bytes $originalBytes), (Format-Bytes $newBytes), $reduction)
} else {
$failCount++
Show-Text ("FAIL : {0}" -f $inputFile)
}
}
Write-Progress -Activity "Converting images to JPG" -Completed
$totalReduction = 0
if ($totalOriginalBytes -gt 0) {
$totalReduction = Get-ReductionPercent $totalOriginalBytes $totalNewBytes
}
Show-Text ""
Show-Text "=========================================="
Show-Text " Advanced JPG conversion completed"
Show-Text "=========================================="
Show-Text "Output folder : $outputFolder"
Show-Text "Total files : $total"
Show-Text "Succeeded : $successCount"
Show-Text "Failed : $failCount"
Show-Text "------------------------------------------"
Show-Text ("Original size : {0}" -f (Format-Bytes $totalOriginalBytes))
Show-Text ("Converted size: {0}" -f (Format-Bytes $totalNewBytes))
Show-Text ("Reduction : {0}%" -f $totalReduction)
Show-Text "=========================================="
Pause-End