Linux convert all png to jpg

How to Batch Convert PNG Images to JPG Format in Linux

Although PNG offers a better image quality compared with JPG, the large size factor is usually a concern for Internet users. This is typically why PNG images are converted to JPG.

In this article, you will learn how to convert multiple PNG images to JPG format from the Linux command line. We will use 2 tools, namely; ImageMagick and GraphicsMagick.

Install Imagemagick in Linux

ImageMagick is a free tool for creating, modifying, and converting a wide variety of image file formats including PNG. To install ImageMagick on your Linux machine, run the following command as per your Linux distribution.

$ sudo apt install imagemagick [On Debian/Ubuntu & Mint] $ sudo dnf install ImageMagick [On RHEL/CentOS & Fedora]

Batch Convert PNG to JPG with ImageMagick

First, change into the directory containing the PNG files with the cd command. After that, you may use the command below to list the content of the directory.

Next, run the command below to convert all PNG files in the current directory to JPG.

Now, run the command below again to see the results.

Convert PNG to JPG with ImageMagick

Looking at the file size column in the image above, the difference is clear. As a side note, you can convert a single file as follows.

Feel free to read the manual pages and discover what more you could do with the ImageMagick tool.

Install GraphicsMagick in Linux

GraphicsMagick was derived from ImageMagick but it offers faster processing and better quality. To install GraphicsMagick on your Linux machine, run the following command as per your Linux distribution.

$ sudo apt install graphicsmagick [On Debian/Ubuntu & Mint] $ sudo yum install epel-release [On RHEL/CentOS] $ sudo dnf install GraphicsMagick [On RHEL/CentOS & Fedora]

Batch Convert PNG to JPG with GraphicsMagick

Begin by changing into the directory containing the PNG files and then run the command below to list the content of the directory.

Читайте также:  Руководство работы с линуксом

Now, to convert all PNG files in the current directory to JPG format, run the following command.

$ sudo gm mogrify -format jpeg *.png

And check the results with the following command.

Convert PNG to JPG with GraphicsMagick

If you would like to convert a specific file from PNG to JPEG, you can run the command below.

$ gm convert file.png file.jpeg

To find out what more you can do with the GraphicsMagick tool, run the command below to view the manual pages.

Conclusion

We have shown you how to convert multiple PNG files into JPG format in this guide. While doing this can save you some network bandwidth and storage space, the image quality will be affected.

Источник

4 Ways to Batch Convert Your PNG to JPG and Vice-Versa

In computing, Batch processing is the execution of a series of tasks in a program non-interactively. In this guide will offer you 4 simple ways to batch convert several .PNG images to .JPG and vice-versa using Linux command-line tools.

We will use convert command line tool in all the examples, however, you can as well make use of mogrify to achieve this.

The syntax for using convert is:

$ convert input-option input-file output-option output-file

And for mogrify is:

$ mogrify options input-file

Note: With mogrify, 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.

Below are the various ways to batch convert your all .PNG images to .JPG format, if you want to convert .JPG to .PNG , you can modify the commands according to your needs.

1. Convert PNG to JPG Using ‘ls’ and ‘xargs’ Commands

The ls command allows you to list all your png images and xargs make it possible to build and execute a convert command from standard input to convert all .png images to .jpg .

----------- Convert PNG to JPG ----------- $ ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "$.jpg"' ----------- Convert JPG to PNG ----------- $ ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "$.png"'

Explanation about the options used in the above command.

  1. -1 – flag tells ls to list one image per line.
  2. -n – specifies the maximum number of arguments, which is 1 for the case.
  3. -c – instructs bash to run the given command.
  4. $.jpg – sets the name of the new converted image, the % sign helps to remove the old file extension.
Читайте также:  Захват сетевого трафика linux

Convert PNG to JPG Format in Linux

Convert PNG to JPG Format in Linux

Similarly, you can use above command to convert all your .jpg images to .png by tweaking the above command.

2. Convert PNG to JPG Using GNU ‘Parallel’ Command

GNU Parallel enables 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:

$ sudo apt-get install parallel [On Debian/Ubuntu systems] $ sudo yum install parallel [On RHEL/CentOS and Fedora]

Once Parallel utility installed, you can run the following command to convert all .png images to .jpg format from the standard input.

----------- Convert PNG to JPG ----------- $ parallel convert '<>' '.jpg' . *.png ----------- Convert JPG to PNG ----------- $ parallel convert '<>' '.png' . *.jpg
  1. <> – input line which is a replacement string substituted by a complete line read from the input source.
  2. – input line minus extension.
  3. . – specifies input source, that is the command line for the example above where *png or *jpg is the argument.

Parallel Command - Converts All PNG Images to JPG Format

Parallel Command – Converts All PNG Images to JPG Format

Alternatively, you can as well use ls and parallel commands together to batch convert all your images as shown:

----------- Convert PNG to JPG ----------- $ ls -1 *.png | parallel convert '<>' '.jpg' ----------- Convert JPG to PNG ----------- $ ls -1 *.jpg | parallel convert '<>' '.png'

3. Convert PNG to JPG Using ‘for loop’ Command

To avoid the hustle of writing a shell script, you can execute a for loop from the command line as follows:

----------- Convert PNG to JPG ----------- $ bash -c 'for image in *.png; do convert "$image" "$.jpg"; echo “image $image converted to $.jpg ”; done' ----------- Convert JPG to PNG ----------- $ bash -c 'for image in *.jpg; do convert "$image" "$.png"; echo “image $image converted to $.png ”; done'

Description of each option used in the above command:

  1. -c allows for execution of the for loop statement in single quotes.
  2. The image variable is a counter for number of images in the directory.
  3. For each conversion operation, the echo command informs the user that a png image has been converted to jpg format and vice-versa in the line $image converted to $.jpg”.
  4. “$.jpg” creates the name of the converted image, where % removes the extension of the old image format.

for loop - Convert PNG to JPG Format

for loop – Convert PNG to JPG Format

4. Convert PNG to JPG Using Shell Script

If you do not want to make your command line dirty as in the previous example, write a small script like so:

Читайте также:  Linux terminal are you root

Note: Appropriately interchange the .png and .jpg extensions as in the example below for conversion from one format to another.

#!/bin/bash #convert for image in *.png; do convert "$image" "$.jpg" echo “image $image converted to $.jpg ” done exit 0

Save it as convert.sh and make the script executable and then run it from within the directory that has your images.

$ chmod +x convert.sh $ ./convert.sh

Batch Image Convert Using Shell Script

Batch Image Convert Using Shell Script

In summary, we covered some important ways to batch convert .png images to .jpg format and vice-versa. If you want to optimize images, you can go through our guide that shows how to compress png and jpg images in Linux.

You can as well share with us any other methods including Linux command line tools for converting images from one format to another on the terminal, or ask a question via the comment section below.

Источник

How to Convert PNG to JPG on Ubuntu via Command

This simple tutorial will show you how to convert PNG to JPG in Ubuntu, so that it reduce the memory size and speed up loading image time.

1.) Install the required package by running below command in terminal:

sudo apt-get install imagemagick

2.) Then you can convert an .png image to .jpg format via below command. It takes “ubuntuhandbook.png” in the current directory and creates a JPEG image from it.

convert ubuntuhandbook.png ubuntuhandbook.jpg

You can also specify a compression level for JPEG images.

convert ubuntuhandbook.png -quality 90 ubuntuhandbook.jpg

3.) To convert all PNG files into JPEG files with that same filename but a different suffix. However be warned that if existing file with the same name will be over-written.

permalink

Ji m

I’m a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to remind me outdated tutorial! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1

2 responses to How to Convert PNG to JPG on Ubuntu via Command

How. I paste in the all command and get this stupid error: mogrify: unable to open file `boot.png’ @ error/png.c/ReadPNGImage/3922.

I got the same error. Make sure that you are in the same directory as the images that you want to convert.
It works like a charm to me.

Источник

Оцените статью
Adblock
detector