How to Use Mozilla MozJPEG to Compress Images on CentOS

mozjpegjpeg compressioncentos image optimizationcjpeg commandimage compression tutorial
Published·Modified·

The previous article Multiple Image Compression Solutions on CentOS shared three different compression schemes. This article adds an extra open-source project from Mozilla called mozjpeg, which can effectively compress JPEG images.

Install Mozilla JPEG

Source Code Download: https://github.com/mozilla/mozjpeg/releases

# Install nasm environment
yum -y install build-essential nasm
# Download source code
wget https://github.com/mozilla/mozjpeg/archive/v3.3.1.tar.gz
# Extract
.tar -zxvf v3.3.1.tar.gz
# Enter directory
cd mozjpeg-3.3.1
autoreconf -fiv
# Compile and install
./configure
make && make install

After successful installation, the binary files are located in the /opt/mozjpeg/bin directory. You can directly copy them to the /usr/bin directory for use. The compression mainly uses cjpeg. Enter the following command to copy:

[root@aliyun-sgp ~]# ll /opt/mozjpeg/bin
total 256
-rwxr-xr-x 1 root root 56816 Nov 16 13:42 cjpeg
-rwxr-xr-x 1 root root 45864 Nov 16 13:42 djpeg
-rwxr-xr-x 1 root root 53504 Nov 16 13:42 jpegtran
-rwxr-xr-x 1 root root 13624 Nov 16 13:42 rdjpgcom
-rwxr-xr-x 1 root root 64592 Nov 16 13:42 tjbench
-rwxr-xr-x 1 root root 13624 Nov 16 13:42 wrjpgcom
[root@aliyun-sgp ~]# cp /opt/mozjpeg/bin/cjpeg /usr/bin/

Compress Images

Mozilla JPEG only supports compressing JPEG images. The compression command is as follows:

cjpeg -quality 80 xxx.jpg > xxx_1.jpg
# Or
cjpeg -outfile xxx_1.jpg -quality 80 xxx.jpg

The above command compresses xxx.jpg and saves the compressed file as xxx_1.jpg with a compression quality of 80.

  • -quality: Specifies the compression quality (0-100). If not specified, the default is 75. The larger the value, the faster the efficiency, but the worse the compression quality. It is recommended to keep the default value.
  • -outfile: Saves the compressed image under a different name, or you can use the redirect symbol >.

Other Notes