<commit_before>#!/bin/sh
#
# Image minimizer
# Iteratively resamples image quality to a certain threshold, reducing image filesize but retaining quality similar to the original image
#
# Example usage:
#	./imgmin.sh foo.jpg foo-min2.jpg	# default 2 pct
#	./imgmin.sh foo.jpg foo-min1.jpg 1	# quality within 1 pct
#	./imgmin.sh foo.jpg foo-min2.jpg 2	# quality within 2 pct
#	./imgmin.sh foo.jpg foo-min3.jpg 3	# quality within 3 pct
#	./imgmin.sh foo.jpg			# overwrite original, risky
#
# Author: <NAME> <<EMAIL>>
#
# Requires:
#  Imagemagick tools 'convert' and 'compare' http://www.imagemagick.org/
#
# References:
#   1. "Optimization of JPEG (JPG) images: good quality and small size", Retrieved May 23 2011, http://www.ampsoft.net/webdesign-l/jpeg-compression.html
#   2. "Convert, Edit, Or Compose Images From The Command-Line" In ImageMagick, Retrieved May 24 2011, http://www.imagemagick.org/script/command-line-tools.php
#   3. "Bash Floating Point Comparison", http://unstableme.blogspot.com/2008/06/bash-float-comparison-bc.html

if [ -z $1 ] || [ -z $2 ]; then
	echo "Usage $0 <image> <dst> [quality]"
	exit 1
fi

src=$1
dst=$2
cmpthreshold=${3:-1}

if [ ! -f $src ]; then
	echo "File $src does not exist"
	exit 1
fi

ext=${src##*.}
tmpfile=/tmp/imgmin$$.jpg

qmin=0
qmax=100
# binary search for lowest quality where compare < $cmpthreshold
while [ $qmax -gt $((qmin+1)) ]
do
	q=$(((qmax+qmin)/2))
	convert -quality $q -interlace Line -strip $src $tmpfile
	cmppct=`compare -metric RMSE $src $tmpfile /dev/null 2>&1 | cut -d '(' -f2 | cut -d ')' -f1`
	cmppct=`echo $cmppct*100 | bc`
	if [ `echo "$cmppct > $cmpthreshold" | bc` -eq 1 ]; then
		qmin=$q
	else
		qmax=$q
	fi
	printf "%.2f@%u " $cmppct $q
done<commit_msg>add PATH when launched externally by webserver<commit_after>#!/bin/sh
#
# Image minimizer
# Iteratively resamples image quality to a certain threshold, reducing image filesize but retaining quality similar to the original image
#
# Example usage:
#	./imgmin.sh foo.jpg foo-min2.jpg	# default 2 pct
#	./imgmin.sh foo.jpg foo-min1.jpg 1	# quality within 1 pct
#	./imgmin.sh foo.jpg foo-min2.jpg 2	# quality within 2 pct
#	./imgmin.sh foo.jpg foo-min3.jpg 3	# quality within 3 pct
#	./imgmin.sh foo.jpg			# overwrite original, risky
#
# Author: <NAME> <<EMAIL>>
#
# Requires:
#  Imagemagick tools 'convert' and 'compare' http://www.imagemagick.org/
#
# References:
#   1. "Optimization of JPEG (JPG) images: good quality and small size", Retrieved May 23 2011, http://www.ampsoft.net/webdesign-l/jpeg-compression.html
#   2. "Convert, Edit, Or Compose Images From The Command-Line" In ImageMagick, Retrieved May 24 2011, http://www.imagemagick.org/script/command-line-tools.php
#   3. "Bash Floating Point Comparison", http://unstableme.blogspot.com/2008/06/bash-float-comparison-bc.html

if [ -z $1 ] || [ -z $2 ]; then
	echo "Usage $0 <image> <dst> [quality]"
	exit 1
fi

# needed for webservers
PATH=$PATH:/usr/local/bin:/usr/bin # FIXME: be smarter

src=$1
dst=$2
cmpthreshold=${3:-1}

if [ ! -f $src ]; then
	echo "File $src does not exist"
	exit 1
fi

ext=${src##*.}
tmpfile=/tmp/imgmin$$.jpg

qmin=0
qmax=100
# binary search for lowest quality where compare < $cmpthreshold
while [ $qmax -gt $((qmin+1)) ]
do
	q=$(((qmax+qmin)/2))
	convert -quality $q -interlace Line -strip $src $tmpfile
	cmppct=`compare -metric RMSE $src $tmpfile /dev/null 2>&1 | cut -d '(' -f2 | cut -d ')' -f1`
	cmppct=`echo $cmppct*100 | bc`
	if [ `echo "$cmppct > $cmpthreshold" | bc` -eq 1 ]; then
		qmin=$q
	else
		qmax=$q
	fi
	printf "%.2f@%u " $cmppct $q
done
{'fraction_non_alphanumeric': 0.1546651723171757, 'fraction_numerical': 0.031381339310731296, 'mean_word_length': 3.76, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5639522', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1333, 'n_words': 408}