MicrosoftPowershellTips & TricksWindows 10
How to Convert PNG to JPG in Bulk Using PowerShell
Table of Contents
Introduction
If you need to convert a substantial number of PNG images to the JPG format, doing so manually can be incredibly tedious. PowerShell offers a powerful solution to automate this process, saving you valuable time and effort. In this article, we’ll guide you through the steps of bulk converting PNG images to JPG using a simple PowerShell script.
Prerequisites
- Basic PowerShell knowledge: Some familiarity with basic PowerShell commands and syntax is helpful but not strictly essential.
- Windows operating system: PowerShell is typically pre-installed on Windows systems.
- PNG Images: A collection of PNG images that you want to convert.
Why Convert PNG to JPG?
Here are some compelling reasons to convert PNG images to JPG:
- File Size Reduction: JPG uses lossy compression, meaning it slightly reduces image quality to achieve smaller file sizes. This can save significant storage space, especially for web use.
- Web Compatibility: JPG is a very widely supported format, ensuring compatibility across nearly all browsers and image viewing applications.
- Faster Loading Times: Smaller JPG file sizes result in faster image loading times on websites.
Steps for Bulk Converting PNG to JPG using PowerShell
Open PowerShell: Find PowerShell in the Start menu (it usually comes pre-installed on Windows), right-click, and choose “Run as administrator”.
Write and Execute Your PowerShell Script: Create or open a PowerShell script file (e.g., convert_png_to_jpg.ps1) and paste the following code:
# Specify the directory containing your PNG files
$sourceDirectory = "C:\Your\PNG\Folder\Path"
# Get all PNG files in the specified directory
$pngFiles = Get-ChildItem -Path $sourceDirectory -Filter "*.png"
# Load the necessary .NET assembly
Add-Type -AssemblyName System.Drawing
foreach ($pngFile in $pngFiles) {
$image = [System.Drawing.Image]::FromFile($pngFile.FullName)
$jpgPath = $pngFile.FullName.Replace(".png", ".jpg")
$image.Save($jpgPath, [System.Drawing.Imaging.ImageFormat]::Jpeg)
# Dispose of the original image object (optional)
$image.Dispose()
}
Code Explanation:
- Line 1: Sets the path to the directory containing your PNG files. Replace
"C:\Your\PNG\Folder\Path"
with the actual path. - Line 3: Retrieves all files with the “.png” extension within the specified directory.
- Line 5: Loads the .NET assembly needed for image manipulation.
- Line 7 (Inside loop): Opens the current PNG file as a System.Drawing.Image object.
- Line 8 (Inside loop): Constructs the output JPG filename by replacing the “.png” extension.
- Line 9 (Inside loop): Saves the image in JPG format using the specified quality.
- Line 11 (Inside loop): (Optional) Releases the image object to free up memory.
Run the PowerShell Script:
- In the PowerShell window, navigate to the directory where you saved your script, then type the script’s name and extension:
.\convert_png_to_jpg.ps1
Important Notes:
- This script saves the JPG files in the same directory as your original PNG files.
- You can further customize the script by adding quality control for the JPEG output.