A Bash script that compresses CBR and CBZ comic book files by converting images to WebP format while maintaining optimal quality and proper aspect ratios for two-page spreads.
- Supports both CBR, CBZ and PDF formats - Extracts and repacks comic books
- WebP conversion - Reduces file size by 80-90% while maintaining good quality
- Smart two-page spread detection - Maintains proper height for wide images
- Configurable quality and size - Adjust compression settings as needed
- Multiple conversion methods - Uses
cwebp
when available, falls back tosips
- Cross-platform - Works on macOS with Homebrew packages
-
7-Zip - For extracting CBZ files and creating new archives
brew install sevenzip
-
unrar - For extracting CBR files
brew install carlocab/personal/unrar
-
pdftoppm - For extracting PDF files
-
WebP - For better WebP conversion quality
brew install webp
-
bc - For precise aspect ratio calculations
brew install bc
-
ImageMagick - For better image dimension detection
brew install imagemagick
-
Download the script:
curl -O https://gist.githubusercontent.com/your-username/your-gist-id/raw/compress_comics.sh
-
Make it executable:
chmod +x compress_comics.sh
-
Install required dependencies:
brew install sevenzip carlocab/personal/unrar webp bc imagemagick
./compress_comics.sh <comic_file.cbr|.cbz> [quality]
- comic_file: CBR or CBZ file to compress (required)
- quality: WebP quality from 1-100 (optional, default: 75)
- Higher values = better quality, larger files
- Recommended: 85-95 for high quality, 65-80 for balanced, 40-60 for small files
# Compress with default quality (75)
./compress_comics.sh "My Comic Book.cbr"
# Compress with high quality (90)
./compress_comics.sh "My Comic Book.cbr" 90
# Compress with smaller file size (60)
./compress_comics.sh "Another Comic.cbz" 60
The script will create a new file with _optimized_webp_q{quality}.cbz
suffix in the same directory as the original file.
You can modify these variables at the top of the script:
TARGET_HEIGHT=1800 # All images scaled to this height
WEBP_CONVERSION_METHOD="cwebp" # "cwebp" or "sips"
Note: WebP quality is now controlled via the command line parameter rather than editing the script.
- Extraction - Uses
unrar
for CBR files,7zz
for CBZ files - Image Analysis - Detects image dimensions and aspect ratios
- Consistent Height Scaling - All images (covers, regular pages, spreads) are scaled to the same height for uniform appearance
- WebP Conversion - Converts all images to WebP format with specified quality
- Progress Tracking - Shows a real-time progress bar during processing
- Repacking - Creates a new CBZ file with optimized images
- Cleanup - Removes temporary files
The script processes all images consistently:
- Height Uniformity: All images are scaled to the same height (
TARGET_HEIGHT=1800
) for consistent appearance - Aspect Ratio Preservation: Images maintain their original proportions during scaling
- Two-Page Spread Detection: Wide images (aspect ratio > 1.3) are detected for logging purposes but processed the same way
- Progress Display: Shows a visual progress bar with percentage completion
- Original CBR/CBZ: Kept unchanged
- Optimized CBZ: Created with
_optimized_webp_q{quality}.cbz
suffix - Typical compression: 80-95% file size reduction depending on quality setting
- Quality: Adjustable from 1-100 via command line parameter
Original: My Comic Book.cbr (800 MB)
Quality 75: My Comic Book_optimized_webp_q75.cbz (50 MB) - 94% reduction
Quality 90: My Comic Book_optimized_webp_q90.cbz (85 MB) - 89% reduction
-
"unrar not found"
brew install carlocab/personal/unrar
-
"7zz not found"
brew install sevenzip
-
Poor WebP quality
- Use higher quality parameter (e.g.,
./compress_comics.sh file.cbr 90
) - Install
webp
for better conversion:brew install webp
- Use higher quality parameter (e.g.,
-
Two-page spreads look wrong
- Install
bc
for better calculations:brew install bc
- Install
imagemagick
for better dimension detection:brew install imagemagick
- Install
- Process smaller comics first to test quality settings
- Use SSD storage for faster temporary file operations
- Start with quality 75, then adjust up (90+) or down (60-) based on results
- Higher quality values significantly increase processing time and file size
This script is provided as-is for personal use. Feel free to modify and distribute.
Improvements and bug fixes are welcome! Please test thoroughly before submitting changes.