172
69
Does anyone know a good way to batch-convert a bunch of PNGs into JPGs in linux? (I'm using Ubuntu).
A png2jpg binary that I could just drop into a shell script would be ideal.
172
69
Does anyone know a good way to batch-convert a bunch of PNGs into JPGs in linux? (I'm using Ubuntu).
A png2jpg binary that I could just drop into a shell script would be ideal.
247
Your best bet would be to use Imagemagick
I am not an expert in the actual usage, but I know you can pretty much do anything image related with this!
An example is:
convert image.png image.jpg
and it will keep the original as well as creating the converted image. As for batch. I think you need to use the Mogrify tool (from the same command line when in imagemagick). Keep in mind that this overwrites the old images.
The command is:
mogrify -format jpg *.png
83
I have a couple more solutions.
The simplest solution is like most already posted. A simple bash for loop.
for i in *.png ; do convert "$i" "${i%.*}.jpg" ; done
For some reason I tend to avoid loops in bash so here is a more unixy xargs approach, using bash for the name-mangling.
ls -1 *.png | xargs -n 1 bash -c 'convert "$0" "${0%.*}.jpg"'
The one I use. It uses GNU Parallel to run multiple jobs at once, giving you a performance boost. It is installed by default on many systems and is almost definitely in your repo (it is a good program to have around).
ls -1 *.png | parallel convert '{}' '{.}.jpg'
The number of jobs defaults to the number of processes you have. I found better CPU usage using 3 jobs on my dual-core system.
ls -1 *.png | parallel -j 3 convert '{}' '{.}.jpg'
And if you want some stats (an ETA, jobs completed, average time per job...)
ls -1 *.png | parallel --eta convert '{}' '{.}.jpg'
There is also an alternative syntax if you are using GNU Parallel.
parallel convert '{}' '{.}.jpg' ::: *.png
And a similar syntax for some other versions (including debian).
parallel convert '{}' '{.}.jpg' -- *.png
If you have a directory with more than 10,000 png images in it... the ls command will likely fail. So, this command works in those situations: find . -type f -name '*.png' | parallel --eta convert '{}' '{.}.jpg'
– Ahi Tuna – 2018-10-03T13:45:38.490
2
+1 for correct bash string expansion in the for, if I could give you another upvote for mentioning parallel, I would. There's one typo, however - you need a done
at the end of that for loop. Also, for the parallel stuff, you could avoid using that ls
and pipe with a construct like: parallel -j 3 --eta convert '{}' '{.}.jpg' ::: *.png
(see here)
Fixed typo. That is a cool syntax that I didn't know of. I don't know which one I like better for probably the same reason I prefer not to use loops in bash. I put it the solution because it is probably the more "proper" way but I'll probably stick with the ls
method for myself because it makes more sense to me. – Kevin Cox – 2013-01-28T14:04:23.260
1...although it should be noted that that syntax only works on GNU parallel. The parallel that's packaged in some linux distros (like Debian & Ubuntu) is actually a different version with a slightly different syntax (use --
rather than :::
) - and even then, it frustratingly lacks some of the features of GNU parallel. – evilsoup – 2013-01-28T14:17:13.653
(though those on distros that don't package GNU parallel can install it from source quite easily, using the instructions here)
– evilsoup – 2013-01-28T14:24:58.770I think I should change it back then so that it works with as many versions as possible. – Kevin Cox – 2013-01-28T18:16:36.267
26
The convert
command found on many Linux distributions is installed as part of the ImageMagick suite. Here's the bash code to run convert
on all PNG files in a directory and avoid that double extension problem:
for img in *.png; do
filename=${img%.*}
convert "$filename.png" "$filename.jpg"
done
just remember it's case sensitive. my camera name it as *.JPG and didn't realize this in first instance. – tsenapathy – 2016-03-27T01:07:55.007
2You can use bash expansion to improve that command like: for f in *.png; do convert "$f" "${f/%png/jpg}"; done
– evilsoup – 2013-01-28T02:57:44.130
8According to the man page for convert: "The convert program is a member of the ImageMagick(1) suite of tools." – nedned – 2009-11-16T04:06:57.943
1You are correct. For some reason I thought it was part of a different library. Either way the code I posted above is the correct way to automate batch conversion within a directory. – Marcin – 2009-11-16T04:17:05.333
11
For those who just want the simplest commands:
Convert and keep original files:
mogrify -format jpg *.png
Convert and remove original files:
mogrify -format jpg *.png && rm *.png
Kinda late to the party, but just to clear up all of the confusion for someone who may not be very comfortable with cli, here's a super dumbed-down reference and explanation.
Example Directory
bar.png
foo.png
foobar.jpg
Keeps all original png files as well as creates jpg files.
mogrify -format jpg *.png
Result
bar.png
bar.jpg
foo.png
foo.jpg
foobar.jpg
Explanation
-format
option. (From the site: This tool is similar to convert except that the original image file is overwritten (unless you change the file suffix with the -format option)
)- format
option specifies that you will be changing the format, and the next argument needs to be the type (in this case, jpg).*.png
is the input files (all files ending in .png).Converts all png files to jpg, removes original.
mogrify -format jpg *.png && rm *.png
Result
bar.jpg
foo.jpg
foobar.jpg
Explanation
&&
is a boolean operator. In short:
0
means no errors.&&
performs short circuit evaluation, the right part will only be performed if there were no errors. This is useful because you may not want to delete all of the original files if there was an error converting them.rm
command deletes files.Now here's some goodies for the people who are comfortable with the cli.
If you want some output while it's converting files:
for i in *.png; do mogrify -format jpg "$i" && rm "$i"; echo "$i converted to ${i%.*}.jpg"; done
Convert all png files in all subdirectories and give output for each one:
find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; echo "Converted $i to ${i%.*}.jpg"; done
Convert all png files in all subdirectories, put all of the resulting jpgs into the all
directory, number them, remove original png files, and display output for each file as it takes place:
n=0; find . -iname '*.png' | while read i; do mogrify -format jpg "$i" && rm "$i"; fn="all/$((n++)).jpg"; mv "${i%.*}.jpg" "$fn"; echo "Moved $i to $fn"; done
Probably the best answer provided you get rid of the while read
part (replace it or remove it all together)... – don_crissti – 2015-10-26T12:07:49.323
@don_crissti, what's wrong with while read? – Steven Jeffries – 2015-10-26T13:47:52.800
It's error prone (unless you're 100% sure you're dealing with sane file names) and slow (like in very, very, very slow). – don_crissti – 2015-10-26T14:01:44.703
What is the default JPG quality, and how can file timestamps be preserved? – Dan Dascalescu – 2017-10-17T07:24:44.353
@DanDascalescu The methods above (except the last one) will preserve filenames, but replace their extension, so timestamped files should be okay (always make a copy and test first). According to ImageMagick, "The default is to use the estimated quality of your input image if it can be determined, otherwise 92" (http://www.imagemagick.org/script/command-line-options.php#quality) Quality can be specified with -quality <number> where <number> is 1 to 100.
– Steven Jeffries – 2017-10-17T21:31:08.840Thanks. mogrify -format jpg foo.png
created foo.jpg with the current timestamp though. I wanted to copy the timestamp of foo.png into foo.jpg, and I don't see anything of that nature in the options.
8
The actual "png2jpg
" command you are looking for is in reality split into two commands called pngtopnm
and cjpeg
, and they are part of the netpbm
and libjpeg-progs
packages, respectively.
png2pnm foo.png | cjpeg > foo.jpeg
6
find . -name "*.png" -print0 | xargs -0 mogrify -format jpg -quality 50
1Thanks for a deep/recursive directory one-line solution which leaves the resulting *.jpg
files next to the original *.png
files, shows how to reduce file size/quality and doesn't break because of any odd characters in directory or file name. – Joel Purra – 2014-12-28T15:21:54.903
5
my quick solution
for i in $(ls | grep .png); do convert $i $(echo $i.jpg | sed s/.png//g); done
2This has got to be one of the ugliest, most convoluted command-lines I've ever seen – evilsoup – 2013-01-28T02:56:05.500
1@evilsoup honestly, this is elegant for shell scripts. Claiming it is convoluted isn't fair. – Max Howell – 2013-11-05T17:31:19.247
8@MaxHowell man. No. Here would be an elegant version of this: for f in ./*.png; do convert "$f" "${f%.*}.jpg"; done
. That avoids the completely unnecessary ls
, grep
and sed
calls (and echo
, but IIRC that's a bash builtin and so will have no/very little performance impact), and gets rid of two pipes and two subshells, and involves less typing. It's even slightly more portable, since not all versions of ls
are safe to parse. – evilsoup – 2013-11-06T12:08:03.327
@evilsoup I stand corrected! Good job. – Max Howell – 2013-11-06T14:58:28.263
4
Many years too late, there's a png2jpeg utility specifically for this purpose, which I authored.
Adapting the code by @Marcin:
#!/bin/sh
for img in *.png
do
filename=${img%.*}
png2jpeg -q 95 -o "$filename.jpg" "$filename.png"
done
3
For batch processing:
for img in *.png; do
convert "$img" "$img.jpg"
done
You will end up with file names like image1.png.jpg though.
This will work in bash, and maybe bourne. I don't know about other shells, but the only difference would likely be the loop syntax.
1
This is what I use to convert when the files span more than one directory. My original one was TGA to PNG
find . -name "*.tga" -type f | sed 's/\.tga$//' | xargs -I% convert %.tga %.png
The concept is you find
the files you need, strip off the extension then add it back in with xargs
. So for PNG to JPG, you'd change the extensions and do one extra thing to deal with alpha channels namely setting the background (in this example white, but you can change it) then flatten the image
find . -name "*.png" -type f | sed 's/\.png$//' | xargs -I% convert %.png -background white -flatten %.jpg
5From
mogrify
documentation: "*This tool is similiar toconvert
except that the original image file is overwritten (unless you change the file suffix with the -format option) with any changes you request.*" – janko-m – 2014-09-18T17:07:06.143If you need to compress your png's more use
mogrify -quality 75 -format jpg *.png
– Flatron – 2016-06-20T07:43:21.023Me too on Ubuntu Desktop 16 today - original files remain – Nam G VU – 2016-08-09T08:34:49.093
1png images with transparent background does not convert properly to jpg. – vishnu – 2011-11-28T04:33:12.287
5To convert PNG's with transparent background, use the following command:
mogrify -format jpg -background black -flatten *.png
– hyperknot – 2012-06-26T18:54:57.3477Awesome, that's exactly what I was after and will be using again. By the way, just to clarify as I didn't realise this is what you meant: convert is used to generate a separate output file, mogrify is used to modify the original image. – nedned – 2009-11-16T04:11:19.900
I wonder how can this overwrite original files if the filename changes... in fact jpg files keep untouched ;) Ah, +1 – neurino – 2013-07-18T20:33:00.373
@neurino The file name isn't changed in the mogrify example. The files still have a
.png
extension but are jpeg images. Try usingfile img.png
(after running mogrify) and it will tell you that it is a jpeg image. – Kevin Cox – 2013-08-27T01:55:28.3934@KevinCox on my linux box after
mogrify -format jpeg img.png
I have 2 files andfile img.*
reports one png, the original untouched, and a new jpeg one. Somogrify
does not overwrite original files in this case. – neurino – 2013-08-28T06:54:05.527@neurino Interesting. From the mogrify man page: "mogrify - resize an image, blur, crop, despeckle, dither, draw on, flip, join, re-sample, and much more. Mogrify overwrites the original image file, whereas, convert(1) writes to a different image file." So I don't know why that happens on your box. – Kevin Cox – 2013-08-28T13:25:46.310
@neurino This happens on my box as well. Now I'm really confused, it this a bug? Or an undocumented feature? – Kevin Cox – 2013-08-28T13:31:49.120