如何在 Linux 中优化和压缩 jpeg 或 png 图片
您是否希望在将图片上传到任何云存储或本地存储之前优化图片或压缩图片,同时又不损失其原始质量?有很多可用的图形用户界面应用程序或 Web 应用程序可以帮助您优化图片。本文将帮助您在 Linux 命令行中优化和压缩 JPEG 或 PNG 图片。
使用 jpegoptim
jpegoptim 是一款简单的实用程序,用于在不损失质量的情况下优化 JPEG 图片。它提供了无损优化和基于设置的“有损”优化,并具有最大质量因子。
要安装 jpegoptim,请使用以下命令:
$ sudo apt-get install jpegoptim
示例输出应如下所示:
The following packages were automatically installed and are no longer required: gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386 libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386 libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386 libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386 libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386 libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386 libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386 libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386 libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386 libgphoto2-port10:i386 libgssapi-krb5-2:i386 libgssapi3-heimdal:i386 libgstreamer-plugins-base0.10-0:i386 libgstreamer0.10-0:i386 libhcrypto4-heimdal:i386 libheimbase1-heimdal:i386 libheimntlm0-heimdal:i386 libhx509-5-heimdal:i386 libice6:i386 libieee1284-3:i386 libjack-jackd2-0:i386 libjbig0:i386 libjpeg-turbo8:i386 libjpeg8:i386 libk5crypto3:i386 libkeyutils1:i386 libkrb5-26-heimdal:i386 libkrb5-3:i386 libkrb5support0:i386 liblcms2-2:i386 libldap-2.4-2:i386 libllvm3.6:i386 libltdl7:i386 libmouse-perl libmpg123-0:i386 libnet-dropbox-api-perl libogg0:i386 libopenal1:i386 liborc-0.4-0:i386 libosmesa6:i386 libp11-kit-gnome-keyring:i386 libp11-kit0:i386 libpciaccess0:i386 libpulse0:i386 libroken18-heimdal:i386 libsamplerate0:i386 libsane:i386 libsasl2-2:i386 libsasl2-modules:i386 libsasl2-modules-db:i386 libsm6:i386 libsndfile1:i386 libspeexdsp1:i386 libsqlite3-0:i386 libssl1.0.0:i386 libstdc++6:i386 libtasn1-6:i386 libtiff5:i386 libtxc-dxtn-s2tc0:i386 .........................
使用 Jpegoptim 图片优化器
使用以下命令优化 tp1.jpg 文件:
$ jpegoptim /home/linux/Desktop/tp1.jpg
示例输出应如下所示:
/home/linux/Desktop/tp1.jpg 1024x744 24bit N IPTC Adobe JFIF [OK] 535222 --> 508663 bytes (4.96%), optimized.
上述命令将图片优化到最大可能的尺寸,并减少了 4.96% 的文件大小。但是,您可以将给定图片压缩到特定大小,但这会禁用无损优化。
例如,将上述图片压缩到大约 250K,使用以下命令:
$ jpegoptim --size=250k /home/linux/Desktop/tp1.jpg
示例输出应如下所示:
/home/linux/Desktop/tp1.jpg 1024x744 24bit N IPTC JFIF [OK] 508663 --> 249866 bytes (50.88%), optimized.
要查找文件大小,请使用以下命令:
$ du -sh /home/linux/Desktop/tp1.jpg
示例输出应如下所示:
248K/home/linux/Desktop/tp1.jpg
jpegoptim 命令将图片压缩到 248k 的大小。如果需要,您可以更改图片的大小。
批量 JPEG 图片压缩和优化
jpegoptim 可以压缩批量图片,现在选择要压缩的图片文件夹,如下所示:
$ cd /home/linux/Desktop/img /Desktop/img$ ls -l
上述命令显示所有图片的列表,如下所示:
total 2188 -rw-rw-r-- 1 linux linux 32616 Feb 27 10:46 Converted_file_1a46a9ef.jpg -rw-rw-r-- 1 linux linux 35925 Feb 27 10:46 Converted_file_96eba41d.jpg -rw-rw-r-- 1 linux linux 50600 Feb 27 10:46 Converted_file_dd3307c9.jpg -rw-rw-r-- 1 linux linux 5083 Feb 27 10:48 LOGO122.png -rw-rw-r-- 1 linux linux 2047570 Feb 27 12:39 nexus_6_0134.jpg -rw-rw-r-- 1 linux linux 36364 Feb 27 10:45 unnamed.jpg
然后,您可以运行以下命令一次压缩所有 JPEG 图片。
/Desktop/img$ jpegoptim --size=250k *.jpg
示例输出应如下所示:
Converted_file_1a46a9ef.jpg 480x800 24bit N JFIF [OK] 32616 --> 32616 bytes (0.00%), skipped. Converted_file_96eba41d.jpg 480x800 24bit N JFIF [OK] 35925 --> 35925 bytes (0.00%), skipped. Converted_file_dd3307c9.jpg 480x800 24bit N JFIF [OK] 50600 --> 50600 bytes (0.00%), skipped. nexus_6_0134.jpg 1607x2896 24bit N Exif IPTC ICC JFIF [OK] 1889349 --> 260524 bytes (86.21%), optimized. unnamed.jpg 480x800 24bit N JFIF [OK] 36364 --> 24816 bytes (31.76%), optimized.
在上述结果中,它已将 JPG 文件压缩为 250K 大小的文件。
要压缩选定的文件,请使用以下命令:
jpegoptim file_name.jpg file_name1.jpg file_name2.jpg
有关 jpegoptim 的更多详细信息,请使用以下命令:
$ man jpegoptim
示例输出应如下所示:
NAME jpegoptim - utility to optimize/compress JPEG/JFIF files. SYNOPSIS jpegoptim [ options ] [ filenames ] DESCRIPTION jpegoptim is used to optimize/compress jpeg files. Program supports lossless optimization, which is based on optimizing the Huffman tables. And so called "lossy" optimization where in addition to optimizing Huffman tables user can specify upperlimit for image quality. OPTIONS Options may be either the traditional POSIX one letter options, or the GNU style long options. POSIX style options start with a single ``-'', while GNU long options start with ``--''. Options offered by jpegoptim are the following: -d, --dest= .....................
优化 PNG 图片
OptiPNG 是一款简单的实用程序,用于在不丢失任何信息的情况下优化 PNG 图片。此程序还可以将外部格式(BMP、GIF、PNM 和 TIFF)转换为优化的 PNG,并执行 PNG 完整性检查和校正。
要安装 optiPNG,请使用以下命令:
$ sudo apt-get install optiPNG
示例输出应如下所示:
Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: gcc-4.8-base:i386 libasn1-8-heimdal:i386 libasound2:i386 libasound2-plugins:i386 libasyncns0:i386 libavahi-client3:i386 libavahi-common-data:i386 libavahi-common3:i386 libbit-vector-perl libcapi20-3:i386 libcarp-clan-perl libclass-method-modifiers-perl libcups2:i386 libdata-random-perl libdate-calc-perl libdate-calc-xs-perl libdrm-amdgpu1:i386 libdrm-intel1:i386 libdrm-nouveau2:i386 libdrm-radeon1:i386 libedit2:i386 libelf1:i386 libexif12:i386 libexpat1:i386 libffi6:i386 libflac8:i386 libfontconfig1:i386 libfreetype6:i386 libgcrypt11:i386 libgd-perl libgd3:i386 libgif4:i386 libgl1-mesa-dri-lts-wily:i386 libgl1-mesa-glx-lts-wily:i386 libglapi-mesa-lts-wily:i386 libglib2.0-0:i386 libglu1-mesa:i386 libgnome2-gconf-perl libgnutls26:i386 libgpg-error0:i386 libgphoto2-6:i386 .........................................
要检查 PNG 的实际大小,请使用以下命令
$ ls -lh /home/linux/Desktop/logo.png
示例输出应如下所示:
-rw-rw-r-- 1 linux linux 3.4M Mar 3 12:05 /home/linux/Desktop/logo.png
这里上面图片的实际文件大小是 3.4MB,现在通过运行以下命令压缩此文件:
$ optipng /home/linux/Desktop/logo.png
示例输出应如下所示:
Copyright (C) 2001-2010 Cosmin Truta. ** Processing: /home/linux/Desktop/logo.png 2560x1920 pixels, 3x8 bits/pixel, RGB Input IDAT size = 3506320 bytes Input file size = 3511513 bytes Trying: zc = 9 zm = 8 zs = 0 f = 5IDAT size = 3260520 zc = 9 zm = 8 zs = 1 f = 5IDAT size = 3257566 Selecting parameters: zc = 9 zm = 8 zs = 1 f = 5IDAT size = 3257566 Output IDAT size = 3257566 bytes (248754 bytes decrease) Output file size = 3257623 bytes (253890 bytes = 7.23% decrease)
现在检查 log.png 的大小。使用以下命令:
$ ls -lh /home/linux/Desktop/logo.png
示例输出应如下所示:
-rw-rw-r-- 1 linux linux 3.2M Mar 3 12:07 /home/linux/Desktop/logo.png
如您在以上输出中看到的,tecmint.png 文件的大小已减少了 7.23%。在任何图片查看器应用程序中打开压缩后的图片,您不会发现原始文件和压缩文件之间有任何重大差异。
批量 PNG 图片压缩和优化
要一次压缩批量或多个 PNG 图片,请使用以下命令:
$ cd /home/linux/Desktop/img/ ~/Desktop/img$ optipng *.png
示例输出应如下所示:
Copyright (C) 2001-2010 Cosmin Truta. ** Processing: LOGO122.png 149x90 pixels, 4x8 bits/pixel, RGB+alpha Input IDAT size = 5026 bytes Input file size = 5083 bytes Trying: zc = 9 zm = 8 zs = 0 f = 0IDAT size = 4934 Selecting parameters: zc = 9 zm = 8 zs = 0 f = 0IDAT size = 4934 Output IDAT size = 4934 bytes (92 bytes decrease) Output file size = 4991 bytes (92 bytes = 1.81% decrease)
有关 optipng 的更多详细信息,请使用以下命令:
$ man optipng
示例输出应如下所示:
NAME OptiPNG - Advanced optimization program for Portable Network Graphics (PNG) SYNOPSIS optipng [-? | -h | -help] optipng [options...] files... DESCRIPTION The OptiPNG program shall attempt to optimize PNG files, i.e. reduce their size to a minimum, without losing any information. In addition, this program shall perform a suite of auxiliary functions like integrity checks, metadata recovery and pixmap-to-PNG conversion. The optimization attempts are not guaranteed to succeed. Valid PNG files that cannot be optimized by this program are normally left intact; their size will not grow. The user may request to override this default behavior. ................................
恭喜!现在,您已经了解了“如何在 Linux 命令行中优化和压缩 JPEG 或 PNG 图片”。我们将在下一篇 Linux 文章中学习更多关于此类命令的内容。继续关注!