Single Image Conversions
SwissKnife makes it easy to convert images between different formats with automatic quality optimization.
PNG to JPEG
Convert PNG images to compressed JPEG format:
python solution.py convert photo.png compressed.jpg
Expected Output:
Converting: /path/to/photo.png to /path/to/compressed.jpg
Success: Image conversion successful: /path/to/compressed.jpg
Info: Conversion completed in 0.45 seconds.
JPEG conversion automatically converts images to RGB color mode and applies 85% quality optimization for the best balance between file size and visual quality.
BMP to PDF
Convert bitmap images to PDF documents:
python solution.py convert diagram.bmp vector.pdf
Perfect for archiving scanned documents or creating printable versions of images.
WebP to TIFF
Convert modern WebP images to TIFF format:
python solution.py convert screenshot.webp archive.tiff
Useful when you need compatibility with older systems or professional printing workflows.
GIF to PNG
Extract a static frame from animated GIFs:
python solution.py convert animation.gif static.png
Converting an animated GIF to a static format (PNG, JPEG, etc.) will only preserve the first frame. For full animation preservation, convert to video formats or keep as GIF.
Batch Image Conversion
Convert entire directories of images at once:
python solution.py batch-convert ./photos ./converted .png .jpg
Expected Output:
Info: Found 42 files with extension .png
Info: Converting from /path/to/photos to /path/to/converted, .png → .jpg
Converting: image1.png → image1.jpg
Converting: image2.png → image2.jpg
Converting: image3.png → image3.jpg
...
--------------------------------------------------
Info: Batch conversion completed - Successful: 42
Info: Failed: 0
Info: Output directory: /path/to/converted
Common Batch Scenarios
Convert all WebP images to PNG for compatibility:
python solution.py batch-convert ./webp_images ./png_images .webp .png
Convert all BMP files to optimized JPEG:
python solution.py batch-convert ./bitmaps ./jpegs .bmp .jpg
Create PDFs from all images:
python solution.py batch-convert ./scans ./pdfs .png .pdf
Quality and Optimization
JPEG Optimization
When converting to JPEG/JPG format, SwissKnife automatically:
- Converts images to RGB color mode (required for JPEG)
- Applies 85% quality compression
- Enables optimization flag for smaller file sizes
# From solution.py:118-128
if output_ext in [".jpg", ".jpeg"]:
img = img.convert("RGB")
img.save(output_abs, optimize=True, quality=85)
This provides an excellent balance between file size and visual quality.
PDF Conversion
When converting images to PDF:
- Images are converted to RGB or grayscale (L) mode
- Resolution is set to 100 DPI
- Non-compatible color modes are automatically converted
# From solution.py:122-125
if output_ext == ".pdf":
if img.mode not in ("RGB", "L"):
img = img.convert("RGB")
img.save(output_abs, "PDF", resolution=100.0)
General Optimization
For all other formats (PNG, WebP, TIFF, BMP), the optimize=True flag is enabled to reduce file size without quality loss:
python solution.py convert large_image.png optimized.png
SwissKnife supports these image formats:
- JPG/JPEG - Joint Photographic Experts Group (lossy compression)
- PNG - Portable Network Graphics (lossless compression)
- WEBP - Modern web format (lossy/lossless)
- GIF - Graphics Interchange Format (supports animation)
- BMP - Bitmap (uncompressed)
- TIFF - Tagged Image File Format (professional/archival)
- PDF - Portable Document Format (output only)
Use Cases
1. Compression for Web Use
Reduce image file sizes for faster website loading:
# Convert large PNGs to optimized JPEGs
python solution.py batch-convert ./original_images ./web_images .png .jpg
Result: Typical file size reduction of 60-80% with minimal visible quality loss.
Convert modern formats to widely supported ones:
# Convert WebP to PNG for older browsers
python solution.py convert modern.webp compatible.png
3. Professional Printing
Convert to TIFF for high-quality printing:
# Convert JPEG photos to TIFF for print shop
python solution.py convert photo.jpg print_ready.tiff
4. Document Archival
Create PDF documents from scanned images:
# Convert scanned pages to searchable PDFs
python solution.py batch-convert ./scans ./archive .png .pdf
Convert images to formats optimized for social platforms:
# Convert to JPEG for Instagram/Facebook
python solution.py convert photo.png instagram_post.jpg
Tips for Image Conversion
Use JPEG for photos: For photographs and complex images with many colors, JPEG provides the best compression ratio. The automatic 85% quality setting balances file size and visual fidelity.
Use PNG for graphics: For screenshots, logos, diagrams, and images with text, PNG’s lossless compression preserves sharp edges and text clarity.
Preserve originals when experimenting: Use the --preserve-original flag when testing different formats to keep your source files safe.
Color mode conversions are automatic: SwissKnife automatically handles color mode conversions (RGBA to RGB, CMYK to RGB, etc.) based on the output format requirements.
- Image conversions are typically very fast (<1 second for most images)
- Batch conversions process files sequentially
- Large images (>4000x4000 pixels) may take 2-3 seconds each
- PDF conversion adds minimal overhead (~0.1 seconds)
File Size Expectations
| Conversion | Typical Size Change |
|---|
| PNG → JPEG | 60-80% reduction |
| BMP → PNG | 70-90% reduction |
| PNG → WebP | 20-40% reduction |
| JPEG → PNG | 20-50% increase |
| Any → PDF | Similar to original |