Last active
January 29, 2025 10:28
-
-
Save pleabargain/f859c50439845da913775bf54d2be25d to your computer and use it in GitHub Desktop.
automatically reduces images by 10 25 and 50 percent using powershell
This file contains hidden or 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
# =================================================================== | |
# Image Resize Function for PowerShell | |
# =================================================================== | |
# | |
# DESCRIPTION: | |
# This script creates multiple resized versions of images while preserving | |
# aspect ratios. By default, it creates 10%, 25%, 50%, and 75% versions. | |
# | |
# INSTALLATION: | |
# 1. Save this script as 'install-resize-function.ps1' | |
# 2. Open PowerShell and navigate to the script location | |
# 3. Run the installation: | |
# .\install-resize-function.ps1 | |
# 4. Restart PowerShell or reload your profile: | |
# . $PROFILE | |
# | |
# USAGE: | |
# Basic usage (process all images in current directory): | |
# Resize-Images . | |
# | |
# Specify a different directory: | |
# Resize-Images -FolderPath "C:\Pictures" | |
# | |
# Custom resize percentages: | |
# Resize-Images . -ResizeFactors @(0.15, 0.30, 0.60) | |
# | |
# Specific file types only: | |
# Resize-Images . -FileExtensions @(".jpg", ".png") | |
# | |
# SUPPORTED FORMATS: | |
# Default: .jpg, .jpeg, .png, .bmp, .gif | |
# | |
# OUTPUT: | |
# Creates new files with format: originalname_XXpct.extension | |
# Example: photo_10pct.jpg, photo_25pct.jpg, etc. | |
# | |
# NOTE: | |
# Original images remain unchanged | |
# =================================================================== | |
# Function to be added to PowerShell profile | |
function Resize-Images { | |
param( | |
[Parameter(Mandatory=$true)] | |
[string]$FolderPath = ".", | |
[Parameter(Mandatory=$false)] | |
[string[]]$FileExtensions = @(".jpg", ".jpeg", ".png", ".bmp", ".gif"), | |
[Parameter(Mandatory=$false)] | |
[double[]]$ResizeFactors = @(0.10, 0.25, 0.50, 0.75) | |
) | |
# Load Windows.Forms assembly for image processing | |
Add-Type -AssemblyName System.Drawing | |
# Convert relative path to absolute path | |
$FolderPath = Resolve-Path $FolderPath | |
# Verify the folder path exists | |
if (-not (Test-Path -Path $FolderPath)) { | |
Write-Error "The specified folder path does not exist: $FolderPath" | |
return | |
} | |
# Get all images in the specified directory | |
$images = Get-ChildItem -Path $FolderPath | Where-Object { $FileExtensions -contains $_.Extension.ToLower() } | |
if ($images.Count -eq 0) { | |
Write-Warning "No supported image files found in: $FolderPath" | |
Write-Host "Supported formats: $($FileExtensions -join ', ')" | |
return | |
} | |
# Show summary before processing | |
Write-Host "`nFound $($images.Count) images to process:" | |
$images | ForEach-Object { Write-Host "- $($_.Name)" } | |
Write-Host "`nWill create versions at these sizes: $($ResizeFactors | ForEach-Object { "$($_ * 100)%" })" | |
Write-Host "`nPress Enter to continue or Ctrl+C to cancel..." | |
Read-Host | Out-Null | |
foreach ($image in $images) { | |
try { | |
# Load the original image | |
$originalImage = [System.Drawing.Image]::FromFile($image.FullName) | |
$originalWidth = $originalImage.Width | |
$originalHeight = $originalImage.Height | |
Write-Host "`nProcessing $($image.Name) ($originalWidth x $originalHeight)..." | |
# Process each resize factor | |
foreach ($factor in $ResizeFactors) { | |
# Calculate new dimensions while preserving aspect ratio | |
$newWidth = [int]($originalWidth * $factor) | |
$newHeight = [int]($originalHeight * $factor) | |
# Create new bitmap with calculated dimensions | |
$newImage = New-Object System.Drawing.Bitmap($newWidth, $newHeight) | |
$graphics = [System.Drawing.Graphics]::FromImage($newImage) | |
# Set high quality resize settings | |
$graphics.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic | |
$graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality | |
$graphics.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality | |
# Draw resized image | |
$graphics.DrawImage($originalImage, 0, 0, $newWidth, $newHeight) | |
# Generate new filename with size percentage | |
$percentage = [int]($factor * 100) | |
$newFileName = Join-Path $FolderPath ("{0}_{1}pct{2}" -f $image.BaseName, $percentage, $image.Extension) | |
# Save the resized image | |
$newImage.Save($newFileName) | |
# Clean up resources | |
$graphics.Dispose() | |
$newImage.Dispose() | |
Write-Host " Created: $($image.BaseName)_${percentage}pct$($image.Extension) ($newWidth x $newHeight)" | |
} | |
# Clean up original image object | |
$originalImage.Dispose() | |
} | |
catch { | |
Write-Host "Error processing $($image.Name): $_" -ForegroundColor Red | |
} | |
} | |
Write-Host "`nImage processing complete!" -ForegroundColor Green | |
} | |
# Installation script (save this part as install-resize-function.ps1) | |
$profilePath = $PROFILE.CurrentUserAllHosts | |
$functionContent = Get-Content $MyInvocation.MyCommand.Path -Raw | |
# Create PowerShell profile if it doesn't exist | |
if (-not (Test-Path -Path $profilePath)) { | |
New-Item -ItemType File -Path $profilePath -Force | |
} | |
# Add the function to the profile if it's not already there | |
$currentProfile = Get-Content $profilePath -Raw | |
if (-not ($currentProfile -like "*function Resize-Images*")) { | |
Add-Content -Path $profilePath -Value "`n$functionContent" | |
Write-Host "Resize-Images function has been added to your PowerShell profile at: $profilePath" | |
Write-Host "Please restart PowerShell or run '. $profilePath' to load the new function" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment