Get Updates by Email

Saturday, 28 December 2013

Converting and Renaming PNG to JPG under Bash

I frequently scan documents in XSane, and save them in PNG format.
Later, I usually try to convert them to JPG.

The first step to convert these files to JPG is using the command line command.
for $a in *.png; do convert "$a" "$a.jpg"; done

This converts "abc.png" to "abc.png.jpg", which is a JPG file.

The next step is to rename all files with the ending ".png.jpg" to just ".jpg".
for a in *.png.jpg; do mv "$a" "${a%.jpg}"; done

This makes "abc.png.jpg" get renamed to "abc.jpg".

Caution: Before deleting the original PNG's, please compare the quality of the original PNG's with the new JPG's. It may be that ImageMagick didn't convert the documents well.