Batch converting PNG to JPG in linux

09
2013-08
  • humble coffee

    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.

  • Answers
  • evilsoup

    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  
    
  • Matt Ryall

    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
    
  • Teddy

    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
    
  • evilsoup

    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 'echo "$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).

    parallel convert '{}' '{.}.jpg' ::: *.png
    

    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.

    parallel -j 3 convert '{}' '{.}.jpg' ::: *.png
    

    And if you want some stats (an ETA, jobs completed, average time per job...)

    parallel --eta convert '{}' '{.}.jpg' ::: *.png
    

    The final command that I use looks like this for refrence. I use ls rather than parallel's syntax for who-knows-what reason.

    ls -1 *.png | parallel -j 3 --eta convert '{}' '{.}.jpg'
    
  • Jeffrey Aylesworth

    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.

  • max

    my quick solution for i in $(ls | grep .png); do convert $i $(echo $i.jpg | sed s/.png//g); done


  • Related Question

    jpeg - Is there still any reason to prefer GIF over PNG when creating images for web pages?
  • Chris W. Rea

    Long ago, I used to avoid the PNG image format when building web pages because browser support was lacking. I remember having a PNG on a page could cause the QuickTime plugin to be loaded - yuck.

    Today, every modern browser now supports PNG well and I'm using it in my web pages because:

    • Compared to JPEG, PNG compression isn't lossy. Especially important for logos/drawings/charts.
    • Compared to GIF, PNG isn't limited to 256-colors. Matters a lot with gradients.

    In other words, with PNG, my images start looking sharp, and stay that way.

    I can still see JPEG being useful because it compresses much better and loses very little visual quality for photos in particular.

    And so my question is: What use cases remain for using the GIF file format in web pages? Is GIF now completely obsolete by PNG and its adoption, or are there specific things GIF is still good at?


  • Related Answers
  • porneL

    As far as I know, these are the only reasons:

    • AnimGIF. This animation works everywhere. There's APNG format, but it's not as widely supported, and has no real editing tools.

    • 1x1 transparent graphic, assuming you care whether it's 80b or 120b. That's virtually the only case in which GIF gives smaller file size than PNG.

    • If you don't have proper tools for saving and optimizing PNG files. PNG files may contain color profiles and gamma correction information, which on the web cause more problems than they solve. It's best to have tool that removes this, making files smaller and more interoperable.

    IE6 support is a red herring

    The confusion comes from the fact that there are several color formats in PNG, and some of them are fully supported, and some of them are not.

    1. Paletted PNG with 1-bit transparency (like GIF): work perfectly in IE6. Without any hacks.

    2. Paletted PNG with 8-bit transparency (not possible in GIF): work partially in IE6 (degrade to 1-bit).

    3. True-color PNG with 8-bit transparency (not possible in GIF): don't work in IE6 without hacks.

    I'm 100% sure. I've tested it extensively. I'm using them on my websites. I've even worked on software that manipulates those files.

    In short: if picture can be saved as non-animated GIF, it will certainly work as PNG, in all browsers, without hacks.

  • 8088

    They're useful for plugin-free animation, like Camtasia screencasts.

    enter image description here

    From: http://www.codinghorror.com/blog/archives/000583.html

  • A Dwarf

    Is there still any reason to prefer GIF over PNG when creating images for web pages?


    Only that PNG is not fully supported by some older browsers like IE 6.

    According to MarketShare IE 6.0 still holds an impressive 24% of the browser market share, making it the most used browser version. No doubt this is caused by the corporate adoption and traditional slow upgrade policies. But it's a reality you must be aware of.


    Note however that if you create flat PNGs, you should be fine. It's alpha-channel and transparent PNGs that are not supported.