To modify the ConvertTo-Jpeg.ps1 script to save JPEG images with a target file size of approximately 200KB, you'll need to adjust the quality parameter of the JPEG encoding. The relationship between quality and file size isn't linear, so you might need to experiment with different quality values to achieve the desired file size.
Here's the modified script with a quality parameter that should get you close to 200KB for typical images:
<#
.SYNOPSIS
Converts images to JPEG format with a target file size of approximately 200KB.
.DESCRIPTION
This script converts various image formats to JPEG with a quality setting that aims for ~200KB file size.
#>
param (
[Parameter(Mandatory=$true)]
[string[]]$Path,
[int]$Quality = 80,
[switch]$Recurse,
[switch]$Force,
[switch]$WhatIf
)
# Load required assembly
Add-Type -AssemblyName System.Drawing
function ConvertTo-Jpeg {
param (
[string]$InputPath,
[int]$Quality,
[switch]$Force,
[switch]$WhatIf
)
try {
$outputPath = [System.IO.Path]::ChangeExtension($InputPath, ".jpg")
if ((Test-Path $outputPath) -and (-not $Force)) {
Write-Warning "Output file already exists: $outputPath"
return
}
if ($WhatIf) {
Write-Host "What if: Would convert $InputPath to $outputPath with quality $Quality"
return
}
# Load the image
$image = [System.Drawing.Image]::FromFile($InputPath)
# Create JPEG encoder parameters
$encoderParams = New-Object System.Drawing.Imaging.EncoderParameters
$encoderParams.Param[0] = New-Object System.Drawing.Imaging.EncoderParameter([System.Drawing.Imaging.Encoder]::Quality, $Quality)
# Get JPEG encoder
$jpegEncoder = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders() | Where-Object { $_.FormatID -eq [System.Drawing.Imaging.ImageFormat]::Jpeg.Guid }
# Save as JPEG with specified quality
$image.Save($outputPath, $jpegEncoder, $encoderParams)
# Get the actual file size
$fileInfo = Get-Item $outputPath
$fileSizeKB = [math]::Round($fileInfo.Length / 1KB, 2)
Write-Host "Converted $InputPath to $outputPath (Size: $fileSizeKB KB, Quality: $Quality)"
# Dispose the image
$image.Dispose()
}
catch {
Write-Error "Error processing $InputPath : $_"
}
}
# Process each input path
foreach ($item in $Path) {
if (Test-Path $item -PathType Container) {
# It's a directory
$files = Get-ChildItem -Path $item -File | Where-Object { $_.Extension -notin '.jpg', '.jpeg' }
if ($Recurse) {
$files = Get-ChildItem -Path $item -File -Recurse | Where-Object { $_.Extension -notin '.jpg', '.jpeg' }
}
foreach ($file in $files) {
ConvertTo-Jpeg -InputPath $file.FullName -Quality $Quality -Force:$Force -WhatIf:$WhatIf
}
}
else {
# It's a file
ConvertTo-Jpeg -InputPath $item -Quality $Quality -Force:$Force -WhatIf:$WhatIf
}
}
Key Changes Made:
- Added a
-Quality parameter with a default value of 80 (which should typically produce files around 200KB for standard images)
- Modified the script to display the actual file size in KB after conversion
- Kept the original functionality but focused on the quality parameter which directly affects file size
Usage Notes:
-
The quality value of 80 is a starting point. You may need to adjust this based on your specific images:
- For larger images, you might need a lower quality value (e.g., 70-75)
- For smaller images, you might need a higher quality value (e.g., 85-90)
-
To use the script with a different quality setting:
.\ConvertTo-Jpeg.ps1 -Path "your_image.png" -Quality 75
-
The script will show you the actual file size after conversion, which helps you determine if you need to adjust the quality parameter.
-
For batch processing, you might want to add logic to automatically adjust the quality based on the resulting file size, but that would require more complex modifications.