Skip to content

Commit 772c898

Browse files
committed
Add photo/presize.sh
1 parent 6b035a7 commit 772c898

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

photo/presize.sh

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/bin/sh
2+
#
3+
# presize.sh
4+
# Copyright (C) 2019 Jonas Kvinge
5+
#
6+
# This script is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This script is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this script. If not, see <http://www.gnu.org/licenses/>.
18+
#
19+
# Redistributions of this script must retain the above copyright notice.
20+
#
21+
22+
# Resize all JPEG's in the specific directory.
23+
# Always convert to progressive when progressive is set to 1.
24+
25+
sourcedir="/run/media/jonas/MUSIC/"
26+
maxsize=1000
27+
progressive=1
28+
#92666.220695
29+
30+
which sed awk convert identify >/dev/null || exit 1
31+
32+
IFS_DEFAULT=$IFS
33+
IFS=$'\n'
34+
for fullfile in $(find $sourcedir)
35+
do
36+
37+
IFS=$IFS_DEFAULT
38+
39+
if [ -d "$fullfile" ] ; then
40+
continue
41+
fi
42+
43+
dir=$(dirname "$fullfile")
44+
#diresc=$(echo "$dir" | sed 's/ /\\\ /g')
45+
imagename=$(echo "$fullfile" | awk -F/ '{print $NF}')
46+
#imagenameesc=$(echo "$imagename" | sed 's/ /\\\ /g')
47+
48+
image=0
49+
imagenamelow=$(echo "$imagename" | tr '[:upper:]' '[:lower:]')
50+
case "$imagenamelow" in
51+
*.jpg ) image=1;;
52+
*.jpeg ) image=1;;
53+
* ) image=0;;
54+
esac
55+
56+
if ! [ "$image" = 1 ] ; then
57+
continue
58+
fi
59+
60+
geometry=$(identify "${dir}/${imagename}" | sed 's/.*JPEG //g' | awk '{print $1}') || exit 1
61+
if [ "$geometry" = "" ] ; then
62+
echo "ERROR: Cannot determine format and geometry for image: \"$imagename\"."
63+
exit 1
64+
fi
65+
66+
# Geometry can be 563x144+0+0 or 75x98
67+
# we need to get rid of the plus (+) and the x characters:
68+
width=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $1}') || exit 1
69+
if [ "$width" = "" ] ; then
70+
echo "ERROR: Cannot determine width for image: \"$imagename\"."
71+
exit 1
72+
fi
73+
height=$(echo "$geometry" | sed 's/[^0-9]/ /g' | awk '{print $2}') || exit 1
74+
if [ "$height" = "" ] ; then
75+
echo "ERROR: Cannot determine height for image: \"$imagename\"."
76+
exit 1
77+
fi
78+
#echo "$imagename ${width}x${height}"
79+
if [ "$height" -gt "$maxsize" ] || [ "$width" -gt "$maxsize" ]; then
80+
echo "Resizing $imagename ${width}x${height}"
81+
convert "$fullfile" -resize ${maxsize}x${maxsize} "$fullfile" || exit 1
82+
fi
83+
84+
file "$fullfile" | grep 'progressive' >/dev/null 2>&1
85+
if ! [ $? -eq 0 ] && [ "$progressive" -eq 1 ]; then
86+
echo "Converting $imagename to progressive."
87+
convert "$fullfile" -interlace plane "$fullfile" || exit 1
88+
fi
89+
90+
done

0 commit comments

Comments
 (0)