I'm looking for a library or algorithm that will make a bitmap as small as it can be without losing information. For instance, given a 600x400 solid black image, I want to get a 3x2 solid black image.

My use case: I have a database of flag images that I want to optimize. Many flags, like France and Italy, are made out of blocks of solid color, so the pngs can be much smaller than the high resolution I use for detailed flags like Spain. I generate the png database by exporting an often-updated database of flag SVGs ripped from Wikimedia. I want to integrate the described lossless resizing into the export pipeline.

I've tried to find this information online, but I'm not really sure how to describe my problem with a few keywords. I keep finding stuff about lossless compression, which is different from lossless resizing.