Skip to content

Instantly share code, notes, and snippets.

@spenceryonce
Created April 29, 2024 07:08
Show Gist options
  • Save spenceryonce/e849800e7d7fcbb8645431f7f8abaa81 to your computer and use it in GitHub Desktop.
Save spenceryonce/e849800e7d7fcbb8645431f7f8abaa81 to your computer and use it in GitHub Desktop.
WEBP to PNG/PPM Batch Script (dwebp)

WEBP to PNG/PPM Converter

Overview

This batch script automates the conversion of WEBP image files to either PNG or PPM format using the dwebp.exe utility. It allows batch conversion of all WEBP files present in the directory from which the script is executed.

Prerequisites

  • dwebp.exe: Ensure you have dwebp.exe downloaded and accessible. This can either be placed in the same directory as the WEBP files or in any directory included in your system's PATH.

Setup

  1. Download dwebp.exe:

    • Download dwebp.exe from Google's WebP Download page.
    • Place dwebp.exe in the directory where your WEBP files are stored or in a directory that's part of your system PATH.
  2. Prepare the Script:

    • Save the provided batch script as cwebp2png.bat in the same directory where your WEBP files are located.

Usage

To use the script, follow these steps:

  1. Open Command Prompt:

    • Navigate to the directory containing your cwebp2png.bat and your WEBP files.
  2. Run the Script:

    • To convert images to PNG format, type the following command and press Enter:
      cwebp2png.bat
      
    • To convert images to PPM format, type the following command and press Enter:
      cwebp2png.bat -ppm
      

How It Works

  • The script will iterate through all .webp files in the current directory.
  • It will use dwebp.exe to convert each file to the specified format (PNG by default, or PPM if the -ppm flag is used).
  • The script will echo the conversion status of each file and indicate if any file failed to convert.

Troubleshooting

  • dwebp.exe Not Found: Ensure that dwebp.exe is either in the same directory as the batch script and WEBP files or within a directory included in your system's PATH.
  • Conversion Failures: Check the file permissions of the WEBP files and ensure they are not corrupted.

Additional Information

  • The script requires that you run it from a command prompt window where you have the necessary permissions to read and write files.
@echo off
setlocal
REM Determine the directory from which the script is executed
set "current_dir=%cd%"
REM Check if dwebp.exe is available
if not exist "%current_dir%\dwebp.exe" (
echo dwebp.exe not found in the current directory.
exit /b
)
REM Check for command line flag for PPM output
set "output_ext=png"
if "%~1" == "-ppm" set "output_ext=ppm"
REM Convert all WEBP files to the specified image format
for %%f in (*.webp) do (
echo Converting %%f to %%output_ext...
"%current_dir%\dwebp.exe" "%%f" -o "%%~nf.%output_ext%"
if errorlevel 1 echo Failed to convert %%f
)
echo Conversion complete.
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment