144 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
			
		
		
	
	
			144 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			Bash
		
	
	
	
	
	
| #!/bin/bash
 | |
| 
 | |
| usage="\
 | |
| Usage: $0 [-p PREFIX] [-s SUFFIX] [-e EXTENSION] [-g \"GEOMETRY\"] [-q QUALITY] [-v] image1 image2 image3 ...\n
 | |
| Use ImageMagick's convert tool to reduce the size of a selection of image files.\n
 | |
| \n
 | |
|   Options:\n
 | |
|   -p PREFIX\tAdd a prefix to output file name. Can be used to provide a \n
 | |
| \t\tdifferent output folder.\n
 | |
|   -s SUFFIX\tAppend a suffix to the file name, before the extension.\n
 | |
|   -t\t\tReplace the filename with creation time from exif\n
 | |
|   -g \"GEOMETRY\"\tSpecify a desired geometry. Default : \"1920x1080>\" \n
 | |
| \t\t(see https://www.imagemagick.org/Magick++/Geometry.html\n
 | |
| \t\tfor detailled doc)\n
 | |
|   -e EXTENSION\tSpecify a file extension (recognized by Magick).\n
 | |
| \t\tIf not specified, reuse orinal file's extension. \n
 | |
|   -q QUALITY\tSet the jpeg compression rate in %. Default : 85\n
 | |
|   -x\t\tStrip exif metadata. Default off\n
 | |
|   -t\t\tUse exif creation time instead of original filename when renaming.\n
 | |
| \t\tCan be used with -p and -s\n
 | |
|   -S\t\tDeactivate JPEG sampling factor. First level of compression, very little \n
 | |
| \t\tloss in quality. Default on (4:2:0)\n
 | |
|   -m\t\tDeactivate median blurring of the image. This filter can smooth some of\n
 | |
| \t\tthe noise with minimal loss in details. \n
 | |
| \t\tBetter compression, with some loss of quality. Default on.\n
 | |
|   -M SIZE\tSpecify the size of the median blurr (useful for fine-tuning text scans).\n
 | |
|  \t\tDefault: 2\n
 | |
|   -G\t\tApply gaussian blurr to the image. Smooths noise in background or low-light\n
 | |
| \t\timages, but creates a loss of detail. \n
 | |
| \t\tBest compression, most loss of quality. Default off\n
 | |
|   -v\t\tMake verbose\n
 | |
|   -P\t\tPrint progress (for use with zenity --progress)\n
 | |
|   -h\t\tPrint this help and exit.\n"
 | |
| 
 | |
| if [[ $# == 0 ]] ; then echo -e $usage ; exit 0 ; fi
 | |
| 
 | |
| #Preset defaults
 | |
| nopt=1
 | |
| geom="1920x1080>"
 | |
| qual="85"
 | |
| ext="jpg"
 | |
| verbose=false
 | |
| sampling="-sampling-factor 4:2:0"
 | |
| median="2x2-1-1"
 | |
| 
 | |
| while getopts p:s:g:q:M:e:xGmhSvPt option ; do
 | |
| 	case "${option}"
 | |
| 		in
 | |
| 		p) ((nopt+=2)) ; 
 | |
| 			prefix=${OPTARG}
 | |
| 			;;
 | |
| 		s) ((nopt+=2)) ; 
 | |
| 			suffix=${OPTARG}
 | |
| 			;;
 | |
| 		g) ((nopt+=2)) ; 
 | |
| 			geom=${OPTARG}
 | |
| 			;;
 | |
| 		q) ((nopt+=2)) ; 
 | |
| 			qual=${OPTARG}
 | |
| 			;;
 | |
| 		t) ((nopt+=1)) ; 
 | |
| 			time=true;
 | |
| 			;;
 | |
| 		e) ((nopt+=2)) ; 
 | |
| 			EXT=${OPTARG}
 | |
| 			;;
 | |
| 		v) ((nopt+=1)) ; 
 | |
| 			verbose=true;
 | |
| 			;;
 | |
| 		P) ((nopt+=1)) ; 
 | |
| 			progress=0;
 | |
| 			;;
 | |
| 		x) ((nopt+=1)) ; 
 | |
| 			exif=true;
 | |
| 			;;
 | |
| 		S) ((nopt+=1)) ; 
 | |
| 			unset sampling;
 | |
| 			;;
 | |
| 		G) ((nopt+=1)) ; 
 | |
| 			gaussian=true;
 | |
| 			;;
 | |
| 		m) ((nopt+=1)) ; 
 | |
| 			unset median
 | |
| 			;;
 | |
| 		M) ((nopt+=2)) ; 
 | |
| 			a=${OPTARG}
 | |
| 			b=$((a/2))
 | |
| 			median=$a\x$a-$b-$b
 | |
| 			echo $median
 | |
| 			unset a
 | |
| 			unset b
 | |
| 			;;
 | |
| 		h | -help) 
 | |
| 			echo -e $usage ; 
 | |
| 			exit 0
 | |
| 			;;
 | |
| 		*) 
 | |
| 			echo -e $usage ; 
 | |
| 			echo "Unreconized option: ${option} ${OPTARG}"
 | |
| 			exit 0
 | |
| 			;;
 | |
| 	esac
 | |
| done
 | |
| 
 | |
| test $verbose == true && echo -e "Old file \t Old size \t Old res \t New res \t New size \t New file \t Compression"
 | |
| 
 | |
| for f in "${@:$nopt}" ; do
 | |
| 	
 | |
| 	test "$verbose" == true && status="$f \t $(ls -lh $f | cut -d " " -f 5) \t $(identify $f | rev | cut -d " " -f 7 | rev) \t " ; size=$(ls -l "$f" | cut -d " " -f 5)
 | |
| 	
 | |
| 	name="$(echo $f | rev | cut -d '.' -f 2- | rev)"
 | |
| 	ext="${EXT:-$(echo $f | rev | cut -d '.' -f 1 | rev)}"
 | |
| 	
 | |
| 	test "$time" && exif -m -t "DateTime" "$f" > /dev/null && name="$(exif -m -t "DateTime" $f | tr ": " ".-")" || echo "$f : no DateTime found in exif data. Using filename instead."
 | |
| 
 | |
| 	nf="$prefix""$name""$suffix"."$ext"
 | |
| 
 | |
| 	while test -e $nf ; do nf="$prefix""$name""$suffix"_$(((++i)))."$ext" ; done
 | |
| 	unset i
 | |
| 	
 | |
| 	((insize+=$(ls -l "$f" | cut -d " " -f 5)))
 | |
| 	
 | |
| 	convert \
 | |
| 		-auto-orient \
 | |
| 		-define jpeg:dct-method=float \
 | |
| 		$sampling \
 | |
| 		${median:+\-median $median} \
 | |
| 		-interlace Plane \
 | |
| 		${gaussian:+-gaussian-blur 0.1} \
 | |
| 		-colorspace RGB \
 | |
| 		-resize "$geom" \
 | |
| 		-quality "$qual" "$f" "$nf"; 
 | |
| 
 | |
| 	test "$exif" = true && exif --remove --no-fixup -o $nf $nf > /dev/null 
 | |
| 
 | |
| 	((outsize+=$(ls -l "$nf" | cut -d " " -f 5)))
 | |
| 	
 | |
| 	test $verbose == true && echo -e $status"$(identify "$nf" | rev | cut -d " " -f 7 | rev) \t $(ls -lh "$nf" | cut -d " " -f 5) \t "$nf" \t $((100*$(ls -l "$nf" | cut -d " " -f 5)/$size))% "
 | |
| 	
 | |
| 	test $progress && echo $(((++progress * 100 / $#)))
 | |
| done
 | |
| 
 | |
| test $verbose == true && echo -e "Average compression : $((100*outsize/insize))%"
 |