snh_nl
snh_nl

Reputation: 2965

Resize jpeg images only where the quality >80 using either mozjpeg/jpegtran/jpegoptim

Hi I want to resize jpeg image that were uploaded as optimized, but because of a software error all recoded to 100% jpeg quality. (so we upload say 80% quality image and after upload it is 100% quality and the filesize is enormous!)

So how can I - 1st find only jpeg images file that have a quality setting > 80 or better quality setting == 100 - and optimize these images only to a quality setting = 80

I appreciate your help

Upvotes: 0

Views: 926

Answers (2)

Lionel
Lionel

Reputation: 1

Here is a bash script for that: Works very well in 2025 ;)

#!/bin/bash

# Starting directory (default: the current directory)
start_dir=${1:-.}

# Target compression rate
target_quality=80

# Search for all image files in the starting directory and its subdirectories
find "$start_dir" -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) | while read -r file; do
    # Check the current quality of the image
    current_quality=$(identify -format "%Q" "$file" 2>/dev/null)

    # Check if the image quality is higher than the target threshold
    if [ $? -eq 0 ] && [ "$current_quality" -gt "$target_quality" ]; then
        echo "Compressing: $file (Current quality: $current_quality%)"

        # Compress the image to 80% quality while stripping metadata
        jpegoptim --strip-all --max=$target_quality "$file"

        echo "Compression applied: $file"
    else
        echo "Not processed: $file (Current quality: ${current_quality:-Unknown}%)"
    fi
done

Upvotes: 0

Ashish Mehta
Ashish Mehta

Reputation: 158

you can use jpegoptim for compress images using command

jpegoptim --all-progressive --strip-com --strip-exif --strip-iptc --strip-xmp -T1 -m80

Upvotes: 0

Related Questions