Comic book collectors often face the challenge of managing different archive formats and optimizing storage space. Here’s a powerful PowerShell script that automates the conversion of CBR archives to CBZ format while offering optional WebP optimization for improved compression.
Features
- Automatic batch conversion of CBR files to CBZ format
- Optional JPEG to WebP conversion with quality control
- Parallel processing for faster image conversion
- Clean temporary file management
- Execution time tracking
- Support for both single files and batch operations
Prerequisites
- 7-Zip: Required for archive handling
- Download: https://www.7-zip.org/
- Must be added to system PATH
For WebP conversion:
- nConvert: Required for image conversion
- Download: https://www.xnview.com/en/nconvert/
- Needs ‘libwebp.dll’ in ‘Plugins’ folder (included with XnConvert)
- PowerShell 7.1+: Required for parallel processing
- Download: https://microsoft.com/PowerShell
The Script
Save this as Convert-ComicArchives.ps1
:
# Clear host
Clear-Host
# Print info to console
Write-Host "This script will convert all .cbr files in the current directory to .cbz"
Write-Host "Converting jpg to webp (for both .cbr and .cbz) is included interactively as an option.`n"
Write-Host "NOTE 1: 7-Zip is required and the executable (7z.exe) needs to be in the windows PATH variable for this to work. `nhttps://www.7-zip.org/`n"
Write-Host "NOTE 2 (WebP only): nConvert is required for conversion to webp and the executable (nconvert.exe) needs to be in the windows PATH variable for this to work. `nhttps://www.xnview.com/en/nconvert/`n"
Write-Host "NOTE 3 (WebP only): nConvert needs 'libwebp.dll' to be located in 'Plugins' subfolder. This file is included in (for example) XnConvert`nhttps://www.xnview.com/en/xnconvert/`n"
Write-Host "Note 4 (WebP only): To speed up conversion from jpeg to webp, the script is using the 'ForEach-Object -Parallel' method. `nFor this reason, a recent version of PowerShell is needed. Script written with version 7.1 `nhttps://microsoft.com/PowerShell"
# Ask user if jpeg should be converted
$convwebp = Read-Host -Prompt "Press 'y' followed by ENTER to convert jpegs to webp"
# Create and start stopwatch
$stopWatch = New-Object -TypeName System.Diagnostics.Stopwatch
$stopWatch.reset()
$stopWatch.start()
# Set working directories and parameters
$basepath = Get-Location
$cores = 12 # Adjust based on your CPU's logical cores
$temppath = "C:\Temp\Convert-temp"
# Create temp directory if it doesn't exist
if (-not (Test-Path $temppath)) {
New-Item $temppath -ItemType "Directory"
}
# 7zip parameters
$7xoutpath = "-o" + $temppath
$7ainpath = $temppath + "\*"
# Process existing CBZ files if WebP conversion is requested
if ($convwebp -match "[yY]") {
Get-ChildItem -Filter *cbz | ForEach-Object {
if ((7z l $_) -match "jpg") {
7z e $_ $7xoutpath
Push-Location $temppath
Get-ChildItem -Filter *jpg -Recurse |
ForEach-Object -Parallel { nconvert -D -quiet -out webp -q 85 $_ } -ThrottleLimit $using:cores
Pop-Location
Remove-Item $_
7z a ($_.BaseName + ".cbz") $7ainpath -tzip -sdel
}
}
}
# Process CBR files
Get-ChildItem -Filter *cbr | ForEach-Object {
7z e $_ $7xoutpath
if ($convwebp -match "[yY]") {
Push-Location $temppath
Get-ChildItem -Filter *jpg -Recurse |
ForEach-Object -Parallel { nconvert -D -quiet -out webp -q 85 $_ } -ThrottleLimit $using:cores
Pop-Location
}
Remove-Item $_
7z a ($_.BaseName + ".cbz") $7ainpath -tzip -sdel
}
# Display execution time
$stopwatch
Script Configuration
Parallel Processing
The script uses parallel processing for WebP conversion. Adjust the $cores
variable based on your CPU:
$cores = 12 # Set to number of logical cores (e.g., 12 for a 6-core/12-thread CPU)
Temporary Directory
Change the temporary working directory by modifying:
$temppath = "C:\Temp\Convert-temp"
WebP Quality
Adjust the WebP conversion quality (0-100) in the nconvert command:
nconvert -D -quiet -out webp -q 85 $_ # Change 85 to desired quality
Usage
- Save the script to your comics directory
- Open PowerShell in that location
- Execute:
.\Convert-ComicArchives.ps1
- Choose whether to enable WebP conversion when prompted
How It Works
- Initial Setup:
- Verifies/creates temporary directory
- Starts execution timer
- Configures parallel processing
- CBZ Processing (if WebP enabled):
- Scans existing CBZ files for JPEGs
- Extracts archives containing JPEGs
- Converts images to WebP format
- Rebuilds optimized CBZ archives
- CBR Processing:
- Extracts CBR archives
- Optionally converts images to WebP
- Creates new CBZ archives
- Cleans up temporary files
Why Use This Script?
- Format Standardization: Converts all archives to CBZ format
- Space Optimization: WebP typically offers 25-35% better compression than JPEG
- Batch Processing: Handles multiple files automatically
- Safe Operation: Uses temporary directory to prevent data loss
- Performance: Leverages parallel processing for faster conversion
Improvements Over Manual Conversion
- Time Saving: Automates repetitive tasks
- Consistency: Ensures uniform archive format
- Error Prevention: Handles cleanup and verification
- Resource Efficiency: Optimizes CPU usage with parallel processing
Tips
- Back up your files before running the script
- Run on a small test batch first
- Monitor disk space during large batch operations
- Adjust WebP quality based on your needs
Conclusion
This script streamlines comic archive management by automating conversion and optimization tasks. Whether you’re maintaining a small collection or managing a large library, it can save time while ensuring consistent archive formats and efficient storage use.
The parallel processing and temporary file management make it both fast and safe to use, while the optional WebP conversion provides excellent opportunities for space optimization without significant quality loss.