PowerShell Tutorials

How to Convert WebP Images to JPG using Chocolatey and PowerShell

Automate WebP to JPG conversion effortlessly with Chocolatey and PowerShell, ensuring compatibility and saving time.

Introduction

In the world of digital media, image formats play a crucial role in delivering high-quality visuals while minimizing file sizes. WebP is a modern image format developed by Google, designed to provide superior compression and faster loading times compared to traditional formats like JPEG or PNG. However, there are situations where you may need to convert WebP images to the more widely supported JPG format. In this article, we will explore a script that automates the process of converting WebP images to JPG using Chocolatey, a package manager for Windows, and PowerShell, a powerful scripting language.

Convert WebP Images to JPG Format using Chocolatey and PowerShell

# Sets Execution Policy and downloads Chocolatey
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

# Installs Webp converter using Chocolatey
choco install webp

# Specify the path to the directory containing the WebP images
$webpDirectory = "C:\img\webp"

# Specify the path to the directory where you want to save the converted JPG images
$jpgDirectory = "C:\img\jpg"

# Create the output directory if it doesn't exist
if (-not (Test-Path -Path $jpgDirectory)) {
    New-Item -ItemType Directory -Path $jpgDirectory | Out-Null
}

# Get all WebP files from the specified directory
$webpFiles = Get-ChildItem -Path $webpDirectory -Filter "*.webp" -File

# Loop through each WebP file and convert it to JPG format
foreach ($file in $webpFiles) {
    $outputFile = Join-Path -Path $jpgDirectory -ChildPath ($file.BaseName + ".jpg")

    try {
        cwebp $file.FullName -o $outputFile
        Write-Host "Conversion complete for $($file.Name)!"
    }
    catch {
        Write-Host "Error converting $($file.Name): $($_.Exception.Message)"
    }
}

Write-Host "Conversion of WebP images to JPG complete!"
PowerShell

Explanation

The script begins by downloading and installing Chocolatey, a popular package manager for Windows. This allows us to conveniently install the WebP converter tool using the Chocolatey package manager with the command choco install webp.

Next, the script defines two variables: $webpDirectory and $jpgDirectory. These variables hold the paths to the directory containing the WebP images and the directory where the converted JPG images will be saved, respectively. You can modify these paths according to your specific requirements.

To ensure the output directory exists, the script checks if the $jpgDirectory path exists using Test-Path. If the directory does not exist, it creates it using New-Item.

The script then retrieves all WebP files from the specified $webpDirectory using Get-ChildItem with the filter *.webp and the -File parameter to only include files (not directories).

Using a loop, the script iterates through each WebP file found. For each file, it creates the corresponding output file path by appending the .jpg extension to the base name of the original file.

Inside a try block, the script calls the cwebp command, passing the full path of the WebP file and the output file path. This command performs the actual conversion from WebP to JPG format. If the conversion is successful, it displays a message indicating the completion of the conversion. If an error occurs during the conversion, the catch block captures the exception and displays an error message with the file name and the specific error message.

Finally, the script outputs a message indicating the completion of the WebP to JPG conversion process.

Conclusion

Converting WebP images to JPG format can be a time-consuming and tedious task, especially when dealing with a large number of files. However, with the power of automation provided by Chocolatey and PowerShell, we can streamline this process and save valuable time and effort.

The script we have discussed in this article automates the conversion process by leveraging Chocolatey to install the WebP converter tool and PowerShell to handle file operations and execute the conversion commands. By simply specifying the input directory containing the WebP images and the output directory for the converted JPG files, the script takes care of the rest.

Whether you are a web developer optimizing website performance, a graphic designer working with different image formats, or simply someone who needs to convert WebP images to a more widely supported format, this script provides a convenient and efficient solution.

Feel free to copy the entire script provided in this article and adapt it to your specific needs. Enjoy the benefits of automating the conversion of WebP images to JPG format, saving time and resources while ensuring compatibility across various platforms and applications.

I hope this article was helpful! You can find more here: PowerShell Articles

author avatar
Patrick Domingues

Leave a Comment

Stay Informed

Receive instant notifications when new content is released.