{"id":13457,"date":"2019-04-02T04:23:17","date_gmt":"2019-04-02T04:23:17","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=13457"},"modified":"2019-04-02T04:23:17","modified_gmt":"2019-04-02T04:23:17","slug":"how-to-optimize-and-compress-jpeg-or-png-images-in-linux-commandline","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/02\/how-to-optimize-and-compress-jpeg-or-png-images-in-linux-commandline\/","title":{"rendered":"How to Optimize and Compress JPEG or PNG Images in Linux Commandline"},"content":{"rendered":"<p>You have a lot of images, and want to optimize and compress the images without losing its original quality before uploading them to any cloud or local storages? There are plenty of GUI applications available which will help you to optimize the images. However, here are two simple command line utilities to optimize images and they are:<\/p>\n<ol>\n<li><strong>jpegoptim<\/strong>\u00a0\u2013 is a utility to optimize\/compress JPEG files without loosing quality.<\/li>\n<li><strong>OptiPNG<\/strong>\u00a0\u2013 is a small program that optimize PNG images to smaller size without losing any information.<\/li>\n<\/ol>\n<div id=\"attachment_18323\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Compress-and-Optimize-Images-in-Linux.png\" rel=\"attachment wp-att-18323\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18323\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Compress-and-Optimize-Images-in-Linux.png\" alt=\"Compress and Optimize Images in Linux\" width=\"720\" height=\"345\" aria-describedby=\"caption-attachment-18323\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18323\" class=\"wp-caption-text\">Compress and Optimize JPEG and PNG Images in Linux<\/p>\n<\/div>\n<p>Using these two tools, you can either optimize a single or multiple images at a time.<\/p>\n<h3>Compress or Optimize JPEG Images from Command Line<\/h3>\n<p><strong>jpegoptim<\/strong>\u00a0is a command line tool that can be used to optimize and compress JPEG, JPG and JFIF files without losing its actual quality. This tool supports lossless optimization, which is based on optimizing the Huffman tables.<\/p>\n<h4>Install jpegoptim in Linux<\/h4>\n<p>To install\u00a0<strong>jpegoptim<\/strong>\u00a0on your Linux systems, run the following command from your terminal.<\/p>\n<h5>On Debian and it\u2019s Derivatives<\/h5>\n<pre># apt-get install jpegoptim\r\nor\r\n$ sudo apt-get install jpegoptim\r\n<\/pre>\n<h5>On RedHat based Systems<\/h5>\n<p>On RPM based systems like\u00a0<strong>RHEL<\/strong>,\u00a0<strong>CentOS<\/strong>,\u00a0<strong>Fedora<\/strong>\u00a0etc., you need to\u00a0<a href=\"https:\/\/www.tecmint.com\/how-to-enable-epel-repository-for-rhel-centos-6-5\/\" target=\"_blank\" rel=\"noopener\">install and enable EPEL repository<\/a>\u00a0or alternatively, you can install the epel repository directly from the commandline as shown:<\/p>\n<pre># yum install epel-release\r\n# dnf install epel-release    [On <strong>Fedora 22+<\/strong> versions]\r\n<\/pre>\n<p>Next install jpegoptim program from the repository as shown:<\/p>\n<pre># yum install jpegoptim\r\n# dnf install jpegoptim    [On <strong>Fedora 22+<\/strong> versions]\r\n<\/pre>\n<h4>How to Use Jpegoptim Image Optimizer<\/h4>\n<p>The syntax of jpegoptm is:<\/p>\n<pre>$ jpegoptim filename.jpeg\r\n$ jpegoptim [options] filename.jpeg\r\n<\/pre>\n<p>Let\u2019s now compress the following\u00a0<code>tecmint.jpeg<\/code>\u00a0image, but before optimizing the image, first find out the actual size of the image using\u00a0<a href=\"https:\/\/www.tecmint.com\/check-linux-disk-usage-of-files-and-directories\/\" target=\"_blank\" rel=\"noopener\">du command<\/a>\u00a0as shown.<\/p>\n<pre>$ du -sh tecmint.jpeg \r\n\r\n<strong>6.2M\ttecmint.jpeg<\/strong>\r\n<\/pre>\n<p>Here the actual file size is\u00a0<strong>6.2MB<\/strong>, now compress this file by running:<\/p>\n<pre>$ jpegoptim tecmint.jpeg \r\n<\/pre>\n<div id=\"attachment_18314\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-JPEG-Image-in-Linux.png\" rel=\"attachment wp-att-18314\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18314\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-JPEG-Image-in-Linux.png\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-JPEG-Image-in-Linux.png 893w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-JPEG-Image-in-Linux-768x138.png 768w\" alt=\"Optimize JPEG Image in Linux\" width=\"893\" height=\"160\" aria-describedby=\"caption-attachment-18314\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18314\" class=\"wp-caption-text\">Optimize JPEG Image in Linux<\/p>\n<\/div>\n<p>Open the compressed image in any image viewer application, you will not find any major differences. The source and compressed images will have the same quality.<\/p>\n<p>The above command optimizes the images to the maximum possible size. However, you can compress the given image to a specific size to, but it disables the lossless optimization.<\/p>\n<p>For example, let us compress above the image from\u00a0<strong>5.6MB<\/strong>\u00a0to around\u00a0<strong>250k<\/strong>.<\/p>\n<pre>$ jpegoptim --size=250k tecmint.jpeg\r\n<\/pre>\n<div id=\"attachment_18315\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-Image-Fix-Size.png\" rel=\"attachment wp-att-18315\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-18315\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-Image-Fix-Size.png\" sizes=\"auto, (max-width: 889px) 100vw, 889px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-Image-Fix-Size.png 889w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/01\/Optimize-Image-Fix-Size-768x143.png 768w\" alt=\"Optimize Image Fix Size\" width=\"889\" height=\"165\" aria-describedby=\"caption-attachment-18315\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-18315\" class=\"wp-caption-text\">Optimize Image Fix Size<\/p>\n<\/div>\n<h4>Batch JPEG Image Compression and Optimization<\/h4>\n<p>You might ask how to compress the images in the entire directory, that\u2019s not difficult too. Go to the directory where you have the images.<\/p>\n<pre><strong>tecmint@tecmint ~ $ cd img\/<\/strong>\r\n<strong>tecmint@tecmint ~\/img $ ls -l<\/strong>\r\ntotal 65184\r\n-rwxr----- 1 tecmint tecmint 6680532 Jan 19 12:21 DSC_0310.JPG\r\n-rwxr----- 1 tecmint tecmint 6846248 Jan 19 12:21 DSC_0311.JPG\r\n-rwxr----- 1 tecmint tecmint 7174430 Jan 19 12:21 DSC_0312.JPG\r\n-rwxr----- 1 tecmint tecmint 6514309 Jan 19 12:21 DSC_0313.JPG\r\n-rwxr----- 1 tecmint tecmint 6755589 Jan 19 12:21 DSC_0314.JPG\r\n-rwxr----- 1 tecmint tecmint 6789763 Jan 19 12:21 DSC_0315.JPG\r\n-rwxr----- 1 tecmint tecmint 6958387 Jan 19 12:21 DSC_0316.JPG\r\n-rwxr----- 1 tecmint tecmint 6463855 Jan 19 12:21 DSC_0317.JPG\r\n-rwxr----- 1 tecmint tecmint 6614855 Jan 19 12:21 DSC_0318.JPG\r\n-rwxr----- 1 tecmint tecmint 5931738 Jan 19 12:21 DSC_0319.JPG\r\n<\/pre>\n<p>And then run the following command to compress all images at once.<\/p>\n<pre><strong>tecmint@tecmint ~\/img $ jpegoptim *.JPG<\/strong>\r\nDSC_0310.JPG 6000x4000 24bit N Exif  [OK] 6680532 --&gt; 5987094 bytes (10.38%), optimized.\r\nDSC_0311.JPG 6000x4000 24bit N Exif  [OK] 6846248 --&gt; 6167842 bytes (9.91%), optimized.\r\nDSC_0312.JPG 6000x4000 24bit N Exif  [OK] 7174430 --&gt; 6536500 bytes (8.89%), optimized.\r\nDSC_0313.JPG 6000x4000 24bit N Exif  [OK] 6514309 --&gt; 5909840 bytes (9.28%), optimized.\r\nDSC_0314.JPG 6000x4000 24bit N Exif  [OK] 6755589 --&gt; 6144165 bytes (9.05%), optimized.\r\nDSC_0315.JPG 6000x4000 24bit N Exif  [OK] 6789763 --&gt; 6090645 bytes (10.30%), optimized.\r\nDSC_0316.JPG 6000x4000 24bit N Exif  [OK] 6958387 --&gt; 6354320 bytes (8.68%), optimized.\r\nDSC_0317.JPG 6000x4000 24bit N Exif  [OK] 6463855 --&gt; 5909298 bytes (8.58%), optimized.\r\nDSC_0318.JPG 6000x4000 24bit N Exif  [OK] 6614855 --&gt; 6016006 bytes (9.05%), optimized.\r\nDSC_0319.JPG 6000x4000 24bit N Exif  [OK] 5931738 --&gt; 5337023 bytes (10.03%), optimized.\r\n<\/pre>\n<p>You can also compress multiple selected images at once:<\/p>\n<pre><strong>$ jpegoptim DSC_0310.JPG DSC_0311.JPG DSC_0312.JPG <\/strong>\r\nDSC_0310.JPG 6000x4000 24bit N Exif  [OK] 6680532 --&gt; 5987094 bytes (10.38%), optimized.\r\nDSC_0311.JPG 6000x4000 24bit N Exif  [OK] 6846248 --&gt; 6167842 bytes (9.91%), optimized.\r\nDSC_0312.JPG 6000x4000 24bit N Exif  [OK] 7174430 --&gt; 6536500 bytes (8.89%), optimized.\r\n<\/pre>\n<p>For more details about\u00a0<strong>jpegoptim<\/strong>\u00a0tool, check out the man pages.<\/p>\n<pre>$ man jpegoptim \r\n<\/pre>\n<h3>Compress or Optimize PNG Images from Command Line<\/h3>\n<p><strong>OptiPNG<\/strong>\u00a0is a command line tool used to optimize and compress PNG (portable network graphics) files without losing its original quality.<\/p>\n<p>The installation and usage of\u00a0<strong>OptiPNG<\/strong>\u00a0is very similar to\u00a0<strong>jpegoptim<\/strong>.<\/p>\n<h4>Install OptiPNG in Linux<\/h4>\n<p>To install\u00a0<strong>OptiPNG<\/strong>\u00a0on your Linux systems, run the following command from your terminal.<\/p>\n<h5>On Debian and it\u2019s Derivatives<\/h5>\n<pre># apt-get install optipng\r\nor\r\n$ sudo apt-get install optipng\r\n<\/pre>\n<h5>On RedHat based Systems<\/h5>\n<pre># yum install optipng\r\n# dnf install optipng    [On Fedora <strong>22+<\/strong> versions]\r\n<\/pre>\n<p><strong>Note<\/strong>: You must have\u00a0<strong>epel repository<\/strong>\u00a0enabled on your\u00a0<strong>RHEL\/CentOS<\/strong>\u00a0based systems to install\u00a0<strong>optipng<\/strong>\u00a0program.<\/p>\n<h4>How to Use OptiPNG Image Optimizer<\/h4>\n<p>The general syntax of\u00a0<strong>optipng<\/strong>\u00a0is:<\/p>\n<pre>$ optipng filename.png\r\n$ optipng [options] filename.png\r\n<\/pre>\n<p>Let us compress the\u00a0<code>tecmint.png<\/code>\u00a0image, but before optimizing, first check the actual size of the image as shown:<\/p>\n<pre><strong>tecmint@tecmint ~\/img $ ls -lh tecmint.png<\/strong> \r\n-rw------- 1 tecmint tecmint <strong>350K<\/strong> Jan 19 12:54 tecmint.png\r\n<\/pre>\n<p>Here the actual file size of above image is\u00a0<strong>350K<\/strong>, now compress this file by running:<\/p>\n<pre><strong>tecmint@tecmint ~\/img $ optipng tecmint.png<\/strong> \r\nOptiPNG 0.6.4: Advanced PNG optimizer.\r\nCopyright (C) 2001-2010 Cosmin Truta.\r\n\r\n** Processing: tecmint.png\r\n1493x914 pixels, 4x8 bits\/pixel, RGB+alpha\r\nReducing image to 3x8 bits\/pixel, RGB\r\nInput IDAT size = 357525 bytes\r\nInput file size = 358098 bytes\r\n\r\nTrying:\r\n  zc = 9  zm = 8  zs = 0  f = 0\t\tIDAT size = 249211\r\n                               \r\nSelecting parameters:\r\n  zc = 9  zm = 8  zs = 0  f = 0\t\tIDAT size = 249211\r\n\r\nOutput IDAT size = 249211 bytes (108314 bytes decrease)\r\nOutput file size = 249268 bytes (108830 bytes = 30.39% decrease)\r\n<\/pre>\n<p>As you see in the above output, the size of the\u00a0<strong>tecmint.png<\/strong>\u00a0file has been reduced up to\u00a0<strong>30.39%<\/strong>. Now verify the file size again using:<\/p>\n<pre><strong>tecmint@tecmint ~\/img $ ls -lh tecmint.png <\/strong>\r\n-rw-r--r-- 1 tecmint tecmint <strong>244K<\/strong> Jan 19 12:56 tecmint.png\r\n<\/pre>\n<p>Open the compressed image in any image viewer application, you will not find any major differences between the original and compressed files. The source and compressed images will have the same quality.<\/p>\n<h4>Batch PNG Image Compression and Optimization<\/h4>\n<p>To compress batch or multiple PNG images at once, just go the directory where all images resides and run the following command to compress.<\/p>\n<pre><strong>tecmint@tecmint ~ $ cd img\/<\/strong>\r\n<strong>tecmint@tecmint ~\/img $ optipng *.png<\/strong>\r\n\r\nOptiPNG 0.6.4: Advanced PNG optimizer.\r\nCopyright (C) 2001-2010 Cosmin Truta.\r\n\r\n** Processing: Debian-8.png\r\n720x345 pixels, 3x8 bits\/pixel, RGB\r\nInput IDAT size = 95151 bytes\r\nInput file size = 95429 bytes\r\n\r\nTrying:\r\n  zc = 9  zm = 8  zs = 0  f = 0\t\tIDAT size = 81388\r\n                               \r\nSelecting parameters:\r\n  zc = 9  zm = 8  zs = 0  f = 0\t\tIDAT size = 81388\r\n\r\nOutput IDAT size = 81388 bytes (13763 bytes decrease)\r\nOutput file size = 81642 bytes (13787 bytes = 14.45% decrease)\r\n\r\n** Processing: Fedora-22.png\r\n720x345 pixels, 4x8 bits\/pixel, RGB+alpha\r\nReducing image to 3x8 bits\/pixel, RGB\r\nInput IDAT size = 259678 bytes\r\nInput file size = 260053 bytes\r\n\r\nTrying:\r\n  zc = 9  zm = 8  zs = 0  f = 5\t\tIDAT size = 222479\r\n  zc = 9  zm = 8  zs = 1  f = 5\t\tIDAT size = 220311\r\n  zc = 1  zm = 8  zs = 2  f = 5\t\tIDAT size = 216744\r\n                               \r\nSelecting parameters:\r\n  zc = 1  zm = 8  zs = 2  f = 5\t\tIDAT size = 216744\r\n\r\nOutput IDAT size = 216744 bytes (42934 bytes decrease)\r\nOutput file size = 217035 bytes (43018 bytes = 16.54% decrease)\r\n....\r\n<\/pre>\n<p>For more details about\u00a0<strong>optipng<\/strong>\u00a0check man pages.<\/p>\n<pre>$ man optipng\r\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>If you\u2019re a webmaster and wants to serve optimized images over your website or a blog, these tools can be very handy. These tools not only saves the disk space, but also the reduces the bandwidth while uploading the images.<\/p>\n<p>If you know any other better way to achieve same thing, do let us know via comments and don\u2019t forget to share this article on your social networks and support us.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/optimize-and-compress-jpeg-or-png-batch-images-linux-commandline\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You have a lot of images, and want to optimize and compress the images without losing its original quality before uploading them to any cloud or local storages? There are plenty of GUI applications available which will help you to optimize the images. However, here are two simple command line utilities to optimize images and &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/04\/02\/how-to-optimize-and-compress-jpeg-or-png-images-in-linux-commandline\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to Optimize and Compress JPEG or PNG Images in Linux Commandline&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-13457","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=13457"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13457\/revisions"}],"predecessor-version":[{"id":13459,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/13457\/revisions\/13459"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=13457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=13457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=13457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}