Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active November 21, 2025 09:20
Show Gist options
  • Select an option

  • Save vyspiansky/4b701ae2a58c5d0689409304a645362b to your computer and use it in GitHub Desktop.

Select an option

Save vyspiansky/4b701ae2a58c5d0689409304a645362b to your computer and use it in GitHub Desktop.
JPEG batch converter (macOS Terminal)

Convert images to JPEG with quality control via macOS Terminal.

Bash script

File ./convert_to_jpeg.sh content:

#!/bin/bash

# This script converts various image types to JPEG with 75% quality.
# It creates a 'converted_jpeg' directory in the specified folder to store the results.
# Usage: ./convert_to_jpeg.sh --path /path/to/images [--quality 75] [--output converted_jpeg]
# If no path is provided, it uses the current directory.

# Default values
SOURCE_DIR="."
QUALITY=75
OUTPUT_NAME="converted_jpeg"

# Function to show usage
show_usage() {
    echo "Usage: $0 [OPTIONS]"
    echo ""
    echo "Options:"
    echo "  --path PATH        Path to the folder with images (default: current directory)"
    echo "  --quality NUM      JPEG quality 0-100 (default: 75)"
    echo "  --output NAME      Output folder name (default: converted_jpeg)"
    echo "  -h, --help         Show this help message"
    echo ""
    echo "Examples:"
    echo "  $0 --path ~/Pictures/vacation"
    echo "  $0 --path ~/Photos --quality 90 --output high_quality_jpegs"
    exit 1
}

# Parse command line arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --path)
            SOURCE_DIR="$2"
            shift 2
            ;;
        --quality)
            QUALITY="$2"
            shift 2
            ;;
        --output)
            OUTPUT_NAME="$2"
            shift 2
            ;;
        -h|--help)
            show_usage
            ;;
        *)
            echo "Error: Unknown option '$1'"
            echo ""
            show_usage
            ;;
    esac
done

# Verify the source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
    echo "Error: Directory '$SOURCE_DIR' does not exist."
    exit 1
fi

# Validate quality value
if ! [[ "$QUALITY" =~ ^[0-9]+$ ]] || [ "$QUALITY" -lt 0 ] || [ "$QUALITY" -gt 100 ]; then
    echo "Error: Quality must be a number between 0 and 100."
    exit 1
fi

# Convert to absolute path
SOURCE_DIR=$(cd "$SOURCE_DIR" && pwd)

# Create the output directory in the source directory
OUTPUT_DIR="$SOURCE_DIR/$OUTPUT_NAME"
mkdir -p "$OUTPUT_DIR"

echo "Converting images in: $SOURCE_DIR"
echo "Output directory: $OUTPUT_DIR"
echo "Quality: $QUALITY%"
echo "---"

# Counter for converted files
count=0

# Loop through common image types
for file in "$SOURCE_DIR"/*.{png,tiff,tif,heic,gif,bmp,PNG,TIFF,TIF,HEIC,GIF,BMP,webp,WEBP}; do
    # Check if the file actually exists (prevents errors if no files of a certain type are found)
    if [ -f "$file" ]; then
        # Get the base name of the file without the extension
        base_name=$(basename "$file")
        base_name="${base_name%.*}"
        
        echo "Converting '$base_name'..."
        
        # Convert the image
        if sips -s format jpeg -s formatOptions "$QUALITY" "$file" --out "$OUTPUT_DIR/$base_name.jpg" > /dev/null 2>&1; then
            ((count++))
        else
            echo "  Failed to convert $file"
        fi
    fi
done

echo "---"
if [ $count -eq 0 ]; then
    echo "No images found to convert."
else
    echo "Conversion complete! Converted $count image(s)."
    echo "Output saved to: $OUTPUT_DIR"
fi

Basic usage

Convert images in specific folder

./convert_to_jpeg.sh --path ~/Pictures/vacation

Convert images in current directory

./convert_to_jpeg.sh

Show help

./convert_to_jpeg.sh --help

Advanced usage

Custom quality (90%)

./convert_to_jpeg.sh --path ~/Photos --quality 90

Custom output folder name

./convert_to_jpeg.sh --path ~/Images --output my_jpegs

All options combined

./convert_to_jpeg.sh --path ~/Photos/2024 --quality 85 --output optimized_images

Arguments

  • --path: Specify the folder with images
  • --quality: Set JPEG quality (0-100, default: 75)
  • --output: Custom name for output folder (default: converted_jpeg)
  • --help: Display usage instructions

Extra features

  • Validation: Checks that quality is a valid number between 0-100.
  • Error handling: Shows clear error messages for invalid options.

Make the script executable

NOTES

  • Replace {png,tiff,tif,heic,gif,bmp,PNG,TIFF,TIF,HEIC,GIF,BMP,webp,WEBP} with {jpg,jpeg,png,tiff,tif,heic,gif,bmp,JPG,JPEG,PNG,TIFF,TIF,HEIC,GIF,BMP,webp,WEBP} if you want to convert JPEG files as well.

  • Permission needs to be given to the file for it to be run as a program: chmod +x convert_to_jpeg.sh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment