{"id":12605,"date":"2019-03-28T03:33:43","date_gmt":"2019-03-28T03:33:43","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12605"},"modified":"2019-03-28T03:33:43","modified_gmt":"2019-03-28T03:33:43","slug":"4-ways-to-batch-convert-your-png-to-jpg-and-vice-versa","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/4-ways-to-batch-convert-your-png-to-jpg-and-vice-versa\/","title":{"rendered":"4 Ways to Batch Convert Your PNG to JPG and Vice-Versa"},"content":{"rendered":"<p>In computing,\u00a0<strong>Batch<\/strong>\u00a0processing is the\u00a0<a href=\"https:\/\/www.tecmint.com\/using-shell-script-to-automate-linux-system-maintenance-tasks\/\" target=\"_blank\" rel=\"noopener\">execution of a series of tasks<\/a>\u00a0in a program non-interactively. In this guide will offer you 4 simple ways to batch convert several\u00a0<code>.PNG<\/code>\u00a0images to\u00a0<code>.JPG<\/code>\u00a0and vice-versa using Linux command-line tools.<\/p>\n<p>We will use\u00a0<strong>convert<\/strong>\u00a0command line tool in all the examples, however, you can as well make use of\u00a0<strong>mogrify<\/strong>\u00a0to achieve this.<\/p>\n<p>The syntax for using\u00a0<strong>convert<\/strong>\u00a0is:<\/p>\n<pre>$ convert input-option input-file output-option output-file\r\n<\/pre>\n<p>And for\u00a0<strong>mogrify<\/strong>\u00a0is:<\/p>\n<pre>$ mogrify options input-file<\/pre>\n<p><strong>Note<\/strong>: With\u00a0<strong>mogrify<\/strong>, the original image file is replaced with the new image file by default, but it is possible to prevent this, by using certain options that you can find in the man page.<\/p>\n<p>Below are the various ways to batch convert your all\u00a0<code>.PNG<\/code>\u00a0images to\u00a0<code>.JPG<\/code>\u00a0format, if you want to convert\u00a0<code>.JPG<\/code>\u00a0to\u00a0<code>.PNG<\/code>, you can modify the commands according to your needs.<\/p>\n<h3>1. Convert PNG to JPG Using \u2018ls\u2019 and \u2018xargs\u2019 Commands<\/h3>\n<p>The\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-ls-command\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>\u00a0allows you to list all your\u00a0<strong>png<\/strong>\u00a0images and\u00a0<strong>xargs<\/strong>\u00a0make it possible to build and execute a\u00a0<strong>convert<\/strong>\u00a0command from standard input to convert all\u00a0<code>.png<\/code>\u00a0images to\u00a0<code>.jpg<\/code>.<\/p>\n<pre>----------- <strong>Convert PNG to JPG<\/strong> ----------- \r\n$ ls -1 *.png | xargs -n 1 bash -c 'convert \"$0\" \"${0%.png}.jpg\"'\r\n\r\n----------- <strong>Convert JPG to PNG<\/strong> ----------- \r\n$ ls -1 *.jpg | xargs -n 1 bash -c 'convert \"$0\" \"${0%.jpg}.png\"'\r\n<\/pre>\n<p>Explanation about the options used in the above command.<\/p>\n<ol>\n<li><code>-1<\/code>\u00a0\u2013 flag tells\u00a0<strong>ls<\/strong>\u00a0to list one image per line.<\/li>\n<li><code>-n<\/code>\u00a0\u2013 specifies the maximum number of arguments, which is\u00a0<strong>1<\/strong>\u00a0for the case.<\/li>\n<li><code>-c<\/code>\u00a0\u2013 instructs bash to run the given command.<\/li>\n<li><code>${0%.png}.jpg<\/code>\u00a0\u2013 sets the name of the new converted image, the\u00a0<strong>%<\/strong>\u00a0sign helps to remove the old file extension.<\/li>\n<\/ol>\n<div id=\"attachment_23337\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23337\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-in-Linux.png\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-in-Linux.png 923w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-in-Linux-768x402.png 768w\" alt=\"Convert PNG to JPG Format in Linux\" width=\"923\" height=\"483\" aria-describedby=\"caption-attachment-23337\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23337\" class=\"wp-caption-text\">Convert PNG to JPG Format in Linux<\/p>\n<\/div>\n<p>I used\u00a0<code>ls -ltr<\/code>\u00a0command to\u00a0<a href=\"https:\/\/www.tecmint.com\/sort-ls-output-by-last-modified-date-and-time\/\" target=\"_blank\" rel=\"noopener\">list all files by modified date and time<\/a>.<\/p>\n<p>Similarly, you can use above command to convert all your\u00a0<code>.jpg<\/code>\u00a0images to\u00a0<code>.png<\/code>\u00a0by tweaking the above command.<\/p>\n<h3>2. Convert PNG to JPG Using GNU \u2018Parallel\u2019 Command<\/h3>\n<p><strong>GNU Parallel<\/strong>\u00a0enables a user to build and execute shell commands from standard input in parallel. Make sure you have GNU Parallel installed on your system, otherwise install it using the appropriate commands below:<\/p>\n<pre>$ sudo apt-get install parallel     [On <strong>Debian\/Ubuntu<\/strong> systems]\r\n$ sudo yum install parallel         [On <strong>RHEL\/CentOS<\/strong> and <strong>Fedora<\/strong>]\r\n<\/pre>\n<p>Once\u00a0<strong>Parallel<\/strong>\u00a0utility installed, you can run the following command to convert all\u00a0<code>.png<\/code>\u00a0images to\u00a0<code>.jpg<\/code>\u00a0format from the standard input.<\/p>\n<pre>----------- <strong>Convert PNG to JPG<\/strong> ----------- \r\n$ parallel convert '{}' '{.}.jpg' ::: *.png\r\n\r\n----------- <strong>Convert JPG to PNG<\/strong> -----------\r\n$ parallel convert '{}' '{.}.png' ::: *.jpg\r\n<\/pre>\n<p>Where,<\/p>\n<ol>\n<li><code>{}<\/code>\u00a0\u2013 input line which is a replacement string substituted by a complete line read from the input source.<\/li>\n<li><code>{.}<\/code>\u00a0\u2013 input line minus extension.<\/li>\n<li><code>:::<\/code>\u00a0\u2013 specifies input source, that is the command line for the example above where\u00a0<strong>*png<\/strong>\u00a0or\u00a0<strong>*jpg<\/strong>\u00a0is the argument.<\/li>\n<\/ol>\n<div id=\"attachment_23339\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-Parallel-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23339\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-Parallel-Command.png\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-Parallel-Command.png 933w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-Parallel-Command-768x510.png 768w\" alt=\"Parallel Command - Converts All PNG Images to JPG Format\" width=\"933\" height=\"619\" aria-describedby=\"caption-attachment-23339\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23339\" class=\"wp-caption-text\">Parallel Command \u2013 Converts All PNG Images to JPG Format<\/p>\n<\/div>\n<p>Alternatively, you can as well use\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-ls-command\/\" target=\"_blank\" rel=\"noopener\">ls<\/a>\u00a0and\u00a0<strong>parallel<\/strong>\u00a0commands together to batch convert all your images as shown:<\/p>\n<pre>----------- <strong>Convert PNG to JPG<\/strong> ----------- \r\n$ ls -1 *.png | parallel convert '{}' '{.}.jpg'\r\n\r\n----------- <strong>Convert JPG to PNG<\/strong> -----------\r\n$ ls -1 *.jpg | parallel convert '{}' '{.}.png'\r\n<\/pre>\n<h3>3. Convert PNG to JPG Using \u2018for loop\u2019 Command<\/h3>\n<p>To avoid the hustle of writing a shell script, you can execute a\u00a0<code>for loop<\/code>\u00a0from the command line as follows:<\/p>\n<pre>----------- <strong>Convert PNG to JPG<\/strong> ----------- \r\n$ bash -c 'for image in *.png; do convert \"$image\" \"${image%.png}.jpg\"; echo \u201cimage $image converted to ${image%.png}.jpg \u201d; done'\r\n\r\n----------- <strong>Convert JPG to PNG<\/strong> -----------\r\n$ bash -c 'for image in *.jpg; do convert \"$image\" \"${image%.jpg}.png\"; echo \u201cimage $image converted to ${image%.jpg}.png \u201d; done'\r\n<\/pre>\n<p>Description of each option used in the above command:<\/p>\n<ol>\n<li><strong>-c<\/strong>\u00a0allows for execution of the for loop statement in single quotes.<\/li>\n<li>The\u00a0<strong>image<\/strong>\u00a0variable is a counter for number of images in the directory.<\/li>\n<li>For each conversion operation, the\u00a0<a href=\"https:\/\/www.tecmint.com\/echo-command-in-linux\/\" target=\"_blank\" rel=\"noopener\">echo command<\/a>\u00a0informs the user that a png image has been converted to\u00a0<strong>jpg<\/strong>\u00a0format and vice-versa in the line\u00a0<strong>$image<\/strong>\u00a0converted to\u00a0<strong>${image%.png}.jpg<\/strong>\u201d.<\/li>\n<li><strong>\u201c${image%.png}.jpg\u201d<\/strong>\u00a0creates the name of the converted image, where\u00a0<strong>%<\/strong>\u00a0removes the extension of the old image format.<\/li>\n<\/ol>\n<div id=\"attachment_23340\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-for-loop-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23340\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-for-loop-Command.png\" sizes=\"auto, (max-width: 934px) 100vw, 934px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-for-loop-Command.png 934w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Convert-PNG-to-JPG-Using-for-loop-Command-768x543.png 768w\" alt=\"for loop - Convert PNG to JPG Format\" width=\"934\" height=\"660\" aria-describedby=\"caption-attachment-23340\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23340\" class=\"wp-caption-text\">for loop \u2013 Convert PNG to JPG Format<\/p>\n<\/div>\n<h3>4. Convert PNG to JPG Using Shell Script<\/h3>\n<p>If you do not want to make your command line dirty as in the previous example, write a small script like so:<\/p>\n<p><strong>Note<\/strong>: Appropriately interchange the\u00a0<code>.png<\/code>\u00a0and\u00a0<code>.jpg<\/code>\u00a0extensions as in the example below for conversion from one format to another.<\/p>\n<pre>#!\/bin\/bash\r\n#convert\r\nfor image in *.png; do\r\n        convert  \"$image\"  \"${image%.png}.jpg\"\r\n        echo \u201cimage $image converted to ${image%.png}.jpg \u201d\r\ndone\r\nexit 0 \r\n<\/pre>\n<p>Save it as\u00a0<code>convert.sh<\/code>\u00a0and make the script executable and then run it from within the directory that has your images.<\/p>\n<pre>$ chmod +x convert.sh\r\n$ .\/convert.sh\r\n<\/pre>\n<div id=\"attachment_23341\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Batch-Image-Convert-Using-Shell-Script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23341\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Batch-Image-Convert-Using-Shell-Script.png\" sizes=\"auto, (max-width: 934px) 100vw, 934px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Batch-Image-Convert-Using-Shell-Script.png 934w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Batch-Image-Convert-Using-Shell-Script-768x528.png 768w\" alt=\"Batch Image Convert Using Shell Script\" width=\"934\" height=\"642\" aria-describedby=\"caption-attachment-23341\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23341\" class=\"wp-caption-text\">Batch Image Convert Using Shell Script<\/p>\n<\/div>\n<p>In summary, we covered some important ways to batch convert\u00a0<code>.png<\/code>\u00a0images to\u00a0<code>.jpg<\/code>\u00a0format and vice-versa. If you want to optimize images, you can go through our guide that shows\u00a0<a href=\"https:\/\/www.tecmint.com\/optimize-and-compress-jpeg-or-png-batch-images-linux-commandline\/\" target=\"_blank\" rel=\"noopener\">how to compress png and jpg images in Linux<\/a>.<\/p>\n<p>You can as well share with us any other methods including\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-tricks\/\" target=\"_blank\" rel=\"noopener\">Linux command line tools<\/a>\u00a0for converting images from one format to another on the terminal, or ask a question via the comment section below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-image-conversion-tools\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In computing,\u00a0Batch\u00a0processing is the\u00a0execution of a series of tasks\u00a0in a program non-interactively. In this guide will offer you 4 simple ways to batch convert several\u00a0.PNG\u00a0images to\u00a0.JPG\u00a0and vice-versa using Linux command-line tools. We will use\u00a0convert\u00a0command line tool in all the examples, however, you can as well make use of\u00a0mogrify\u00a0to achieve this. The syntax for using\u00a0convert\u00a0is: $ &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/4-ways-to-batch-convert-your-png-to-jpg-and-vice-versa\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;4 Ways to Batch Convert Your PNG to JPG and Vice-Versa&#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-12605","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\/12605","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=12605"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12605\/revisions"}],"predecessor-version":[{"id":12606,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12605\/revisions\/12606"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}