Imagemagick can calculate the best resolution and quality for JPEG type files with the
-define jpeg:extent=SIZE
optional argument.
An example of this would be to reduce the size of a multi-megapixel image by half and targeting a desired maximum file size
convert -scale 50% -define jpeg:extent=512kb large_file.jpg smaller_file.jpg