Note: I have no affiliation with "mozjpeg" or "Mozilla". This script is inspired by the work of u/drinkpainttillufaint.
To use this script you will need the following package installed and fully working :
This Bash script is designed to compress JPG and PNG files into optimized JPG files using the mozjpeg tool. It processes files in a way that keeps the original images intact and provides detailed reports on the sizes of both the original and the compressed images. The script also runs tasks in parallel to speed up the process, taking advantage of multiple CPU cores.
-
Source and Destination Directories:
SOURCE_DIRis set to the current directory (.), where your original images are located.DEST_DIRis set tocompressed_directory, which is where the script will save the compressed images.
-
Counters Initialization:
- The script initializes counters for tracking the number of files processed, the total size of the original images, and the total size of the compressed images.
- It also notes the start time to calculate the total processing duration later.
-
Temporary Files:
- Temporary files are used to keep track of the counters. These files are initialized to zero at the start of the script.
-
Compression Simulation:
- The script includes a function called
simulate_compressionthat runsmozjpegwith different settings to find the best compression parameters. It logs the results to temporary files for further analysis.
- The script includes a function called
-
File Processing:
- The
process_filefunction handles each image file individually. It:- Determines where the compressed file will be saved in the destination directory.
- Creates any necessary directories.
- Logs the file’s original size and name.
- Runs an initial compression test.
- If the compressed file is larger than the original, it skips further processing. Otherwise, it tests various compression settings to find the best result.
- Compresses the image with the best settings and saves it in the destination directory.
- Updates counters with the new size information and logs the results.
- The
-
Parallel Processing:
- To speed things up, the script uses the
findcommand to locate JPG and PNG files and processes them in parallel using theparallelcommand.
- To speed things up, the script uses the
-
Summary and Cleanup:
- After processing all the images, the script reads from the temporary counter files to get final counts and sizes.
- It then prints a summary that includes the number of files processed, total original and compressed sizes, the total reduction in size, and the overall processing time.
- Finally, it cleans up all temporary files used during the process.
-
Initialization:
- The script sets the source and destination directories and prepares temporary files for tracking progress.
-
Simulating Compression:
- The
simulate_compressionfunction tests differentmozjpegsettings to find the optimal compression parameters.
- The
-
Processing Files:
- The
process_filefunction manages each image file by determining where it should be saved, running compression tests, and applying the best settings found.
- The
-
Exporting and Processing:
- It exports necessary variables and functions for use in parallel processing, starts the processing of files, and waits for completion.
-
Final Summary:
- The script prints out a summary of its work, including how many files were processed, the total size reduction, and the time spent.
-
Cleanup:
- It wraps up by deleting temporary files and displaying a final end time.
Overall, this script is a powerful tool for optimizing image storage, combining efficient compression with parallel processing to handle large numbers of files quickly while keeping track of important metrics.