-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.processImages.sh
executable file
·128 lines (97 loc) · 4.69 KB
/
1.processImages.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#! /bin/bash
# This script runs through all folders that have the following structure
#
# BuildingRegister#date#HadBeenExpected
# BuildingRegister : There are two separate registers for different buildings
# (g or b) Blue folder is for the temporary use of the Sibelius Academy -> b
# Green folder for the parliament itself -> g
# Date : Date of the page register. This is hard to retrieve from individual pages
# (YYYY-MM-DD) and therefore has to be inputed in the folder as an extra metadata.
# HadBeenExpected : Register of people who had notified were coming (“Ilmoittautuneet”) -> e
# (ne or e) Register of people who had not been notified (“Ei ilmoittautuneet”) -> ne
#
# Exports: A folder called 'processed_photos' in each of the above where the
# images found are color balanced and rotated if they are portraits.
# *** The rotation is not guaranteed since it is but default clockwise **
# *** But the files have a check_ tag so it is enough to look at them to see if they
# *** are readable.
# Run with ./cleanLobbyPhotos and input the folder to process
# errors appear with '_' in front of the erronous folder names
# Set this to be different for every run of photographs. This will affect the IDs assigned to pages.
# By setting a different base for IDs, there are no collisions between the IDs.
PHOTOGRAPH_DATE='DATE'
function get_page_id {
alphanum=`echo "$1" | tr 0123456789 abcdefghij`
echo "$PHOTOGRAPH_DATE$page_param$alphanum"
}
shopt -s extglob
echo Which folder \do you want to process?
read parentFolder
cd $parentFolder
echo Initial ID?
read page_param
# parentFolder=$PWD
echo $parentFolder
# page_id=page_id
# run this for every folder
for d in */; do
# echo "$d"
cd $d
#get the name of the current directory which contains metadata on the information
currentDir=${PWD##*/}
echo "Trying to process folder: " $currentDir
IFS='#' read -r -a params <<< "$currentDir"
BuildingRegister=${params[0]} # folder g or b
Date=${params[1]} # date YYYY-MM-DD
HadBeenExpected=${params[2]} # ne or e expected or not
# echo $BuildingRegister
# echo $Date
# echo "$HadBeenExpected"
if [[ -z "$HadBeenExpected" ]]
then
echo $currentDir' is missing HadBeenExpected param .. skipping..'
mv "$parentFolder/$d" "$parentFolder/_$d"
elif [[ -z "$Date" ]]; then
echo $currentDir' is missing Date param .. skipping..'
mv "$parentFolder/$d" "$parentFolder/_$d"
elif [[ -z "$BuildingRegister" ]]; then
echo $currentDir' is missing BuildingRegister param .. skipping..'
mv "$parentFolder/$d" "$parentFolder/_$d"
else
mkdir -p processed_photos
# IMAGE PROCESSING MODULE:
for img in *.+(jpg|JPG) ; do
echo $img
# Read its width and height
identify=$(identify "$img")
[[ $identify =~ ([0-9]+)x([0-9]+) ]] || \
{ echo Cannot get size >&2 ; continue ; }
width=${BASH_REMATCH[1]}
height=${BASH_REMATCH[2]}
# Generate new name for the image file
fn_page_id=`get_page_id $page_id`
rr="${Date}#${BuildingRegister}#${HadBeenExpected}#$fn_page_id.jpg"
echo $rr
page_id=$((page_id+1))
# Convert the quality so deep shadows leave
# Do this by blurring a lot until the letters are illegible and then divide the orignal with the outcome
# the result will make the background closer to white!
# Uses ImageMagick
# https://www.imagemagick.org/Usage/compose/#divide
convert "$img" \
\( +clone -blur 0x20 \) \
-compose Divide_Src -composite $rr
# Move it to new folder named 'processed_photos'
mv -f -i "$parentFolder/$d/$rr" "$parentFolder/$d/processed_photos/"
# Check if it needs to be rotated
if (( width < height )) ; then
echo "$img is portrait, turning it clockwise but check it "
rr_rot="check_$rr"
convert "$parentFolder/$d/processed_photos/$rr" -rotate 270 "$parentFolder/$d/processed_photos/$rr"
mv -f -i "$parentFolder/$d/processed_photos/$rr" "$parentFolder/$d/processed_photos/$rr_rot"
fi
done
fi
cd $parentFolder
done
# IMAGE PROCESSING FINISHED NOW TO OCR STUFF