如何在Linux中优化和压缩JPEG或PNG图像
在将图像上传到任何云或本地存储之前,您是否要优化图像或压缩图像而不损失其原始质量?有许多可用的GUI应用程序或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文章中,我们将学习有关这些命令类型的更多信息。继续阅读!