Automating the choice between JPEG and PNG with a script
Posted
by
MHC
on Super User
See other posts from Super User
or by MHC
Published on 2012-11-05T02:31:52Z
Indexed on
2012/11/07
5:03 UTC
Read the original article
Hit count: 592
Choosing the right format to save your images in is crucial for preserving image quality and reducing artifacts. Different formats follow different compression methods and come with their own set of advantages and disadvantages.
JPG, for instance is suited for real life photographs that are rich in color gradients. The lossless PNG, on the other hand, is far superior when it comes to schematic figures:
Picking the right format can be a chore when working with a large number of files. That's why I would love to find a way to automate it.
A little bit of background on my particular use case:
I am working on a number of handouts for a series of lectures at my unversity. The handouts are rich in figures, which I have to extract from PDF-formatted slides. Extracting these images gives me lossless PNGs, which are needlessly large at times.
Converting these particular files to JPEG can reduce their size to up to less than 20% of their original file size, while maintaining the same quality. This is important as working with hundreds of large images in word processors is pretty crash-prone.
Batch converting all extracted PNGs to JPEGs is not an option I am willing to follow, as many if not most images are better suited to be formatted as PNGs. Converting these would result in insignificant size reductions and sometimes even increases in filesize - that's at least what my test runs showed.
What we can take from this is that file size after compression can serve as an indicator on what format is suited best for a particular image. It's not a particularly accurate predictor, but works well enough. So why not use it in form of a script:
I included inotifywait because I would prefer for the script be executed automatically as soon as I drag an extracted image into a folder.
This is a simpler version of the script that I've been using for the last couple of weeks:
#!/bin/bash
inotifywait -m --format "%w%f" --exclude '.jpg' -r -e create -e moved_to --fromfile '/home/MHC/.scripts/Workflow/Conversion/include_inotifywait' | while read file; do mogrify -format jpg -quality 92 "$file"
done
The advanced version of the script would have to
- be able to handle spaces in file names and directory names
- preserve the original file names
- flatten PNG images if an alpha value is set
- compare the file size between the temporary converted image and its original
- determine if the difference is greater than a given precentage
- act accordingly
The actual conversion could be done with imagemagick tools:
convert -quality 92 -flatten -background white file.png file.jpg
Unfortunately, my bash skills aren't even close to advanced enough to convert the scheme above into an actual script, but I am sure many of you can.
My reputation points on here are pretty low, but I will gladly award the most helpful answer with the highest bounty I can set.
References: http://www.formortals.com/introducing-cnb-imageguide/, http://www.turnkeylinux.org/blog/png-vs-jpg
Edit: Also see my comments below for some more information on why I think this script would be the best solution to the problem I am facing.
© Super User or respective owner