How To Convert Png To Jpeg And Vice-Versa? – POFTUT

How To Convert Png To Jpeg And Vice-Versa?

I have a lot of pictures in different formats. But daily used formats are png and jpeg. I want to convert them vice-versa. How can I achieve that?

Install Imagemagick

Imagemagick is a very useful library and tool to work with image files. It is used a lot of popular projects for media-related operations.

$ sudo apt-get install -y imagemagick

Convert Png To Jpg

We will simply convert png extensions to the jpeg format.

$ convert pnggrad8rgb.png pnggrad8rgb.jpg

Convert Multiple Files From Png To Jpg

We will convert multiple files to the jpg by using ls , xargs commands.

$ ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.png}.jpg"'

Convert Multiple Files From Jpg To Png

We can also convert the Jpg files into Png files with the following command. As done previously the name will be kept the same and just the format and the extension will be changed.

$ ls -1 *.jpg | xargs -n 1 bash -c 'convert "$0" "${0%.jpg}.png"'

Convert Using Bash Loop Through Shell

We can use bash loops to loop over image files and run convert command.

$ bash -c 'for image in *.png; do convert "$image" "${image%.png}.jpg"; done'

Convert Using Bash Loop Through With Bash Script

Some times using bash script is better than running shell. We use following script with file name

for image in *.png; do
  convert  "$image"  "${image%.png}.jpg"

To make the script executable run following command.

$ chmod u+x

LEARN MORE  What Is GIF and How To Create and Use GIF?

Leave a Comment