76

I want to convert a batch of images, nearly 100, from jpg to png format. How can I do this without renaming them, but instead actually converting the format?

Evan Carroll
  • 7,743
opu 웃
  • 2,177
  • 11
  • 30
  • 43
  • Use the convert command. No, really. But you will want to rename them from something.jpg to something.png. – Jos Apr 29 '14 at 10:59
  • 1
    @jos, From your comment I don't understand that what should I do!! – opu 웃 Apr 29 '14 at 11:03
  • I wasn't finished editing ;-) First, use the convert command on a single .jpg file and see if it works. Then, write a script that loops over the .jpg files and converts them to .png files. – Jos Apr 29 '14 at 11:05
  • You not mentioned how can I use the convert command on a single .jpg file. And I don't know how to write a script that loops over the .jpg files. – opu 웃 Apr 29 '14 at 11:13
  • You can use a pure in-browser tool for this. With WebAssembly, modern conversion libraries can run directly in your browser. One such tool is: https://clevercompress.com/en – Lars Flieger Oct 14 '24 at 13:32

4 Answers4

107

Try these commands,

mogrify -format png /path/*.jpg    

This will convert all the .jpg files into .png files and saves the converted files in the same directory.

mv /path/*.png ~/Desktop/pic

This will moves all the .png files(converted) to the pic directory which resides on the Desktop.

Disclaimer:

If you want to keep the orientation of your image you have to add -auto-orient to the command. You can find out why here. The mogrify command which keeps the orientation would look like this:

mogrify -auto-orient -format png /path/*.jpg    
Avinash Raj
  • 80,616
  • Should I change the directory? – opu 웃 Apr 29 '14 at 11:14
  • 1
    /path/*.jpg represents the directory where the .jpg files are stored. – Avinash Raj Apr 29 '14 at 11:30
  • I also tried this command after changing the directory: mogrify -format png .jpg This also works nicely. – opu 웃 Apr 29 '14 at 13:38
  • The answer below is better, as it takes into account the limits placed on ARGV. – Evan Carroll May 16 '17 at 02:57
  • There is a great guide here: http://www.algissalys.com/how-to/how-to-quickly-rename-modify-and-scale-all-images-in-a-directory-using-linux Also covers changing the size of the image, which is useful ie. adding -resize 800x changes longest side to 800 :) – Craig Lambie Jul 17 '18 at 03:09
  • 1
    is mogrify command lossless? – Mona Jalal Nov 13 '19 at 16:39
  • This answer can be done in one line by using mogrify's -path option: mogrify -format png -path ~/Desktop/pic /path/*.jpg – Trevor Jan 03 '20 at 22:14
  • @opu the command should be mogrify -format png *.jpg not: mogrify -format png .jpg After format, the next parameter is the format to converted to (in this example: png). Then the next param is the file/files (defined by wildcard *) that mogrify is going to create converted copies from, in this example, the files are determined by *.png - the files in current directory ending in .png will have converted copies of them created,in jpg format,in this example.Apologies opu that I can't use your full user name in my mention -askubuntu not allowing me to enter the symbol in your name. – therobyouknow Apr 18 '20 at 21:05
  • I don't know why but this command flips my image vertically (I'm on Windows). – KeyC0de Apr 30 '20 at 12:34
  • it rotates images – MH.AI.eAgLe Sep 22 '21 at 13:06
65

Using ImageMagick.

First install imagemagick:

sudo apt-get install imagemagick

Try converting just one image at first:

convert image.jpg image.png

Now convert all:

mogrify -format png *.jpg

EDIT

You also need to split it into chunks that will fit to avoid hitting the limit of how much you can put on a command line. This should work better:

find -name '*.jpg' -print0 | xargs -0 -r mogrify -format png

The -print0 and -0 are used to handle spaces in filenames and the -r means don't run mogrify if there's nothing to do.

Source: https://stackoverflow.com/questions/1010261/running-a-batch-with-imagemagick

EDIT 2 Switched png and jpg as per @Glutanimate's comment.

EDIT 3 Changed png to jpg in last suggestion.

Parto
  • 15,685
  • One image converted successfully. But when I used command to convert all its showing mogrify.im6: unable to open image ``*.png': No such file or directory @ error/blob.c/OpenBlob/2638.``mogrify.im6: unable to open file ``*.png' @ error/png.c/ReadPNGImage/3667. – opu 웃 Apr 29 '14 at 11:32
  • You will have to cd to that directory first. If they are in the desktop, run cd ~/Desktop first then try converting again. – Parto Apr 29 '14 at 11:34
  • I changed the directory – opu 웃 Apr 29 '14 at 11:35
  • @Parto I think it should be mogrify -format png *.jpg. The OP is asking about converting jpg → png. – Glutanimate Apr 29 '14 at 13:01
  • did not work for me with 14.04 @Parto – talha06 Jun 18 '16 at 14:48
  • @talha06 What error did you get? – Parto Jun 18 '16 at 20:44
  • sorry, my mistake. It was an issue about the permission. I can confirm it works as it is expected. – talha06 Jun 20 '16 at 06:18
  • why the first command works but not the second one? find . -name '*.png' -exec mogrify -format -quality 100 jpg *.png {} + and 2nd one... find . -name '*.png' -exec convert -units PixelsPerInch -density 300 -quality 100 jpg *.png {} + – Estatistics Mar 26 '22 at 08:30
4

Firstly, convert works. You don't need to test it. Secondly, a bash oneliner suits the need:

$ for file in Ground*jpg; do { \
  echo "Converting $file to `echo $file|cut -d. -f1`.png" ;\
  convert $file `echo $file|cut -d. -f1`.png ; } done

Rockin' it auldskewl ;)

Cheers

0

I know it's been a long time since this question was put but there is one brilliant piece of software that has not been mentioned that I have used a lot.

http://photobatch.wikidot.com/ also known as Phatch. It literally converts anything from anything to anything else in image terms. It had not been updated for a while but now claims to be released for ubuntu 17.10. Give it a try. I'm confident you'll be very happy with it.

  • 2
    Tried to get this software, the "download" link to the deb file is missing from the above link, and the Ubuntu "store" version gave me this error: Detailed errors from the package manager follow: apt transaction returned result exit-failed Ubuntu v 18.10 maybe? Anyway - my experience. – Craig Lambie Jul 17 '18 at 02:54