Skip to content

Commit 07195eb

Browse files
committed
Integration from old HG repos...
1 parent 66ea78e commit 07195eb

File tree

4 files changed

+719
-0
lines changed

4 files changed

+719
-0
lines changed

compress_pictures

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
#!/usr/bin/env bash
2+
# vim: set shiftwidth=4 softtabstop=4 expandtab foldlevel=0 foldmethod=indent: # vim-modline, don't delete
3+
#############################################################################
4+
# $HGFile: compress_pictures $
5+
# $HGLocation: /home/mb/src/tools $
6+
7+
# 1st Author: Massimo
8+
# Created: 2012-10-24
9+
10+
# $HGChecked in by: massimo $
11+
# $HGDate: 2018-01-04 11:30 +0100 $
12+
version='$HGRevision: 9 $'
13+
# $HGBranch: $
14+
# $HGDesc: updates $
15+
#############################################################################
16+
17+
# Checking Bash version
18+
(( ${BASH_VERSINFO[0]} >= 4 )) || die "This script needs at least bash version 4"
19+
20+
## User Configuration #######################################################
21+
declare -A conf tmp
22+
conf[c_nice]="15"
23+
24+
#############################################################################
25+
26+
this_basename="${0##*/}"
27+
this_dirname="${0%/*}"
28+
29+
usage() {
30+
cat <<EOF
31+
This is $0 $version
32+
33+
Usage: $0 [OPTIONS] <target files>
34+
35+
Description
36+
Compressing target picture files.
37+
By default conversion is only done if new size is lower than original. This can be overridden by --force.
38+
39+
Options:
40+
-c, --case Case sensitive, insensitive by default to catch .JPG and .jpg
41+
-r, --resize <num> Resize to xx, only if bigger ( like convert -resize $numx$num\> )
42+
-q, --quality <num> convert quality in %
43+
-k, --keep keep new files and don't overwrite old ones
44+
-f, --force force conversion and skip checking original size
45+
46+
-v, --verbose Explain what commands are being run
47+
-i, --info Show configuration information
48+
-d, --debug Debugging mode
49+
-h, --help Show this help
50+
51+
EOF
52+
}
53+
54+
print_config() {
55+
for key in ${!conf[@]}; do
56+
printf "%-20s = %s\n" "${key#c_}" "${conf[$key]}"
57+
done | sort
58+
}
59+
60+
while [[ $1 == -* ]]; do
61+
case "$1" in
62+
-c|--case) opt_case=1; shift;;
63+
-r|--resize) shift
64+
if [[ $1 == -* || -z $1 ]]; then
65+
die "No resize number given"
66+
fi
67+
opt_resize="$1"
68+
shift;;
69+
-q|--quality) shift
70+
if [[ $1 == -* || -z $1 ]]; then
71+
die "No quality number given"
72+
fi
73+
opt_quality="$1"
74+
shift;;
75+
-k|--keep) opt_keep=1; shift;;
76+
-f|--force) opt_force=1; shift;;
77+
-d|--debug) opt_debug=1; shift;;
78+
-i|--info) opt_info=1; shift;;
79+
-v|--verbose) opt_verbose=1; shift;;
80+
-h|--help|-\?) usage; exit 0;;
81+
# Trying to pass unkown options to convert:
82+
-*) opt_unknown="${opt_unknown} ${1}"
83+
shift
84+
opt_unknown="${opt_unknown} ${1}"
85+
shift;;
86+
esac
87+
done
88+
89+
# Libraries
90+
libs=( output )
91+
for lib in "${libs[@]}"; do
92+
lib_path="${BASH_PREFIX:-/}usr/local/lib/lib_$lib"
93+
if [[ -r lib_$lib ]]; then
94+
source "lib_$lib"
95+
continue
96+
elif [[ -r ${this_dirname}/lib_$lib ]]; then
97+
source "${this_dirname}/lib_$lib"
98+
continue
99+
elif [[ -r "$lib_path" ]]; then
100+
source "$lib_path"
101+
continue
102+
else
103+
echo "$lib_path not found" 1>&2; exit 1;
104+
fi
105+
done
106+
107+
# Checking for binary
108+
identify_bin="$(type -P identify)" || die "identify binary not found"
109+
convert_bin="$(type -P convert)" || die "convert binary not found"
110+
111+
if [[ -z $1 ]]; then
112+
error "No target files given!"
113+
usage
114+
exit 1
115+
fi
116+
117+
[[ -z $opt_case ]] && shopt -s nocaseglob
118+
shopt -s nullglob
119+
120+
tmpdir="${this_basename}.$BASHPID"
121+
mkdir $tmpdir &&
122+
for file in "$@"; do
123+
# File checks:
124+
if [[ ! -f $file ]]; then
125+
warn "$file not found or not a regular file, skipped."
126+
continue
127+
fi
128+
if [[ ! -r $file ]]; then
129+
warn "$file is not readable, skipped."
130+
continue
131+
fi
132+
if ! file "$file" |grep JPEG >/dev/null; then
133+
warn "$file is not a JPEG file, skipped."
134+
continue
135+
fi
136+
137+
file_basename="$(basename "$file")"
138+
dim=($(identify -ping -format "%w %h" "$file"))
139+
width="${dim[0]}"
140+
height="${dim[1]}"
141+
eval_command="$convert_bin \
142+
\"${file}\" \
143+
${opt_verbose:+ -verbose} \
144+
${opt_quality:+ -quality ${opt_quality}%} \
145+
${opt_resize:+ -resize ${opt_resize}x${opt_resize}\>} \
146+
${opt_unkown:+ ${opt_unknown}} \
147+
\"${tmpdir}/${file_basename}\"
148+
"
149+
150+
printf "%s\n=> " "$(identify "$file")"
151+
152+
if [[ -n $opt_resize ]]; then
153+
debug "Resizing file: $file, width:$width, height:$height"
154+
# Only resize if picture is bigger:
155+
if (( $width > $opt_resize || $height > $opt_resize )) || [[ -n $opt_force ]]; then
156+
debug "Evaluating $eval_command"
157+
eval "$eval_command" && [[ -z $opt_keep ]] && printf "<= " && mv -fv "${tmpdir}/${file_basename}" "$file"
158+
else
159+
[[ -n $opt_verbose ]] && echo "Skipping $file, already sized."
160+
fi
161+
else
162+
debug "Evaluating $eval_command"
163+
eval "$eval_command"
164+
fi
165+
echo
166+
done
167+
168+
if [[ -z $opt_keep ]]; then
169+
shopt -s nullglob dotglob
170+
left_tmpfiles=($tmpdir/*)
171+
if (( ${#left_tmpfiles[*]} )); then
172+
error "There are temporary files left: ${left_tmpfiles[@]}"
173+
else
174+
rmdir -v "$tmpdir"
175+
fi
176+
shopt -u nullglob dotglob
177+
fi
178+

0 commit comments

Comments
 (0)