title: Algorithm for determining if a particular image would be smaller as a PNG or JPG
body: <p>I am writing a script to optimize a bunch of images. I want to determine which format to convert the image in — PNG or JPG. Goal is small file size without compromising on quality.</p>
  
  <p>Is there an algorithmic way of determining this? I can convert into both, compare sizes, and discard the bigger one, but I was wondering if there is a way to determine it based on number of colors, etc.</p>