PNG

Install

pkg install pngquant optipng

For example: pngquant --quality 0-1 highlight2.png -o s.png

JPEG

Install jpegotim with:

pkg install jpegoptim

Help doc:

jpegoptim v1.4.6  Copyright (C) 1996-2018, Timo Kokkonen  
Usage: jpegoptim [options] <filenames>  
  
  -d<path>, --dest=<path>  
                    specify alternative destination directory for  
                    optimized files (default is to overwrite originals)  
  -f, --force       force optimization  
  -h, --help        display this help and exit  
  -m<quality>, --max=<quality>  
                    set maximum image quality factor (disables lossless  
                    optimization mode, which is by default on)  
                    Valid quality values: 0 - 100  
  -n, --noaction    don't really optimize files, just print results  
  -S<size>, --size=<size>  
                    Try to optimize file to given size (disables lossless  
                    optimization mode). Target size is specified either in  
                    kilo bytes (1 - n) or as percentage (1% - 99%)  
  -T<threshold>, --threshold=<threshold>  
                    keep old file if the gain is below a threshold (%)  
  -b, --csv         print progress info in CSV format  
  -o, --overwrite   overwrite target file even if it exists (meaningful  
                    only when used with -d, --dest option)  
  -p, --preserve    preserve file timestamps  
  -P, --preserve-perms  
                    preserve original file permissions by overwriting it  
  -q, --quiet       quiet mode  
  -t, --totals      print totals after processing all files  
  -v, --verbose     enable verbose mode (positively chatty)  
  -V, --version     print program version  
  
  -s, --strip-all   strip all markers from output file  
  --strip-none      do not strip any markers  
  --strip-com       strip Comment markers from output file  
  --strip-exif      strip Exif markers from output file  
  --strip-iptc      strip IPTC/Photoshop (APP13) markers from output file  
  --strip-icc       strip ICC profile markers from output file  
  --strip-xmp       strip XMP markers markers from output file  
  
  --all-normal      force all output files to be non-progressive  
  --all-progressive force all output files to be progressive  
  --stdout          send output to standard output (instead of a file)  
  --stdin           read input from standard input (instead of a file)  

Note: backup your images first, it is, by default, will overwrite your original file or use --dest=newDir. Simple use:

jpegoptim sample.jpg

This command reduces ALL .jpg files in the current dir:

jpegoptim --size=150k ./*.jpg

Output:

./upcal_html_dawn_rise.jpg 1080x2400 24bit N JFIF [OK] 817145 --> 154112 bytes (81.14%), optimized..  
  
/upcal_html_main.jpg 1080x2400 24bit N JFIF [OK] 553073 --> 109040 bytes (80.28%), optimized.  

Google more or read the help: jpegoptim --help

jpegoptim function

function resizeIMG() {
# jpegoptim <percent> <files>
perCent=10
outDir=_jpegoptim

if [[ $1 -gt 0 ]]; then
perCent=$1
else
echo reducing to $perCent%
fi
# shift will consume/remove the first arg
# "$@" : then "$@" will have 2nd arg onwards
echo resizing "${@:2}" to $perCent% quality to $outDir
mkdir -p $outDir
jpegoptim --size=$perCent% --overwrite --dest=$outDir "${@:2}"

}