Open jjchawaii opened 1 month ago
To address the user's request for converting WebP files to PNG or JPG, we need to update the batch script, registry setup, and Python script to handle both conversions (to and from WebP). The solution involves:
WebP Converter.bat
Modify the batch script to detect the file type and call the appropriate Python script for conversion.
@echo off
setlocal enabledelayedexpansion
REM Loop through all selected files
for %%A in (%*) do (
REM Get the full path of the input file
set "input_file=%%~dpA%%~nxA"
REM Get the directory of the input file
set "input_dir=%%~dpA"
REM Remove trailing backslash from input_dir
if "!input_dir:~-1!"=="\" set "input_dir=!input_dir:~0,-1!"
REM Get the file extension
set "file_ext=%%~xA"
REM Check if the file is a WebP file
if /I "!file_ext!"==".webp" (
REM Convert the WebP file to PNG or JPG in the same directory
start /min python "C:\WebP Converter\convert_to_webp.py" "!input_file!" "!input_dir!" %2
) else (
REM Convert the input file to WebP in the same directory
start /min python "C:\WebP Converter\convert_to_webp.py" "!input_file!" "!input_dir!" webp
)
)
setup.reg
Add new context menu entries for WebP files to convert them to PNG or JPG.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\shell\WebPConverter]
@="Convert to WebP"
"Icon"="C:\\WebP Converter\\icon.ico"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpg\shell\WebPConverter\command]
@="\"C:\\WebP Converter\\WebP Converter.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.png\shell\WebPConverter]
@="Convert to WebP"
"Icon"="C:\\WebP Converter\\icon.ico"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.png\shell\WebPConverter\command]
@="\"C:\\WebP Converter\\WebP Converter.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpeg\shell\WebPConverter]
@="Convert to WebP"
"Icon"="C:\\WebP Converter\\icon.ico"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.jpeg\shell\WebPConverter\command]
@="\"C:\\WebP Converter\\WebP Converter.bat\" \"%1\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\ConvertToPNG]
@="Convert to PNG"
"Icon"="C:\\WebP Converter\\icon.ico"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\ConvertToPNG\command]
@="\"C:\\WebP Converter\\WebP Converter.bat\" \"%1\" png"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\ConvertToJPG]
@="Convert to JPG"
"Icon"="C:\\WebP Converter\\icon.ico"
[HKEY_CLASSES_ROOT\SystemFileAssociations\.webp\shell\ConvertToJPG\command]
@="\"C:\\WebP Converter\\WebP Converter.bat\" \"%1\" jpg"
convert_to_webp.py
Extend the Python script to handle conversions from WebP to PNG or JPG.
from PIL import Image
import os
import sys
def convert_to_webp(input_files, output_folder):
for input_path in input_files:
try:
with Image.open(input_path) as img:
base_name = os.path.splitext(os.path.basename(input_path))[0]
output_path = os.path.join(output_folder, f"{base_name}.webp")
img.save(output_path, 'webp')
print(f"Converted {input_path} to webp.")
except Exception as e:
print(f"Error converting {input_path}: {str(e)}")
def convert_from_webp(input_files, output_folder, output_format):
for input_path in input_files:
try:
with Image.open(input_path) as img:
base_name = os.path.splitext(os.path.basename(input_path))[0]
output_path = os.path.join(output_folder, f"{base_name}.{output_format}")
img.save(output_path, output_format)
print(f"Converted {input_path} to {output_format}.")
except Exception as e:
print(f"Error converting {input_path}: {str(e)}")
if __name__ == "__main__":
if len(sys.argv) < 4:
print("Usage: python convert_to_webp.py input_file_or_folder output_folder output_format")
sys.exit(1)
input_files = sys.argv[1:-2]
output_folder = sys.argv[-2]
output_format = sys.argv[-1].lower()
if not os.path.exists(output_folder):
os.makedirs(output_folder)
if output_format == 'webp':
convert_to_webp(input_files, output_folder)
elif output_format in ['png', 'jpg', 'jpeg']:
convert_from_webp(input_files, output_folder, output_format)
else:
print(f"Unsupported output format: {output_format}")
sys.exit(1)
This solution ensures that the batch file can handle both types of conversions based on the file type, and the Python script can convert WebP files to PNG or JPG as requested.
Click here to create a Pull Request with the proposed solution
Files used for this task:
Just curious if it's possible to reverse the conversion though. To still have a context menu option that can convert from Webp TO something like PNG or JPG. I am trying to add covert art to thousands of MP4s but the cover art is all in Webp format and my tagging software only recognizes jpg or png.
Hey 👋🏻
You can try my upgraded project on this offc here https://github.com/Code-Parth/OFFC-Offline-File-Converter/tree/main/WINDOWS
And update the setup.reg to set your path manually.
Just curious if it's possible to reverse the conversion though. To still have a context menu option that can convert from Webp TO something like PNG or JPG. I am trying to add covert art to thousands of MP4s but the cover art is all in Webp format and my tagging software only recognizes jpg or png.