-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicrop
executable file
·234 lines (212 loc) · 6.55 KB
/
basicrop
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/sh
# uses X screen selection and fullscreen image viewing to calculate cropped dimensions for an arbitrary image, and then crops.
# no transformations or translations or anything of the image in question will work, as the image viewer cannot easily communicate the location and scale of the image.
# 'c' to crop.
# 'u' to undo.
# 'Escape' or 'q' to quit.
# 'Return' to save as $OUTPUT (overwrites if none specified)
# 'shift' + 'Return' to overwrite regardless.
# 1st arg is input file, 2nd arg is output. overwrites input if 2nd arg not specified.
screenW=$(xrandr --current | grep '\*' | uniq | awk '{print $1}')
screenH=$(echo $screenW | cut -d'x' -f2)
screenW=$(echo $screenW | cut -d'x' -f1)
[ -n "$1" ] && INPUT="$1" || echo "Syntax: \$1 = input file, \$2 = output file"
[ -n "$2" ] && OUTPUT="$2" || OUTPUT="$1"
mkdir -p "/tmp/$USER/basicrop/"
# Launches image viewer with a keylogger
imglauncher() {
# sxiv SUCKS
# nsxiv -fbs f --anti-alias=no "$1" &
feh -F -Z -. --force-aliasing "$1" &
imgPID=$!
echo '
c
echo "crop" >> /tmp/$USER/basicrop/keylog
u
echo "undo" >> /tmp/$USER/basicrop/keylog
q
echo "cancel" >> /tmp/$USER/basicrop/keylog
Escape
echo "cancel" >> /tmp/$USER/basicrop/keylog
Return
echo "save" >> /tmp/$USER/basicrop/keylog
shift + Return
echo "overwrite" >> /tmp/$USER/basicrop/keylog' > "/tmp/$USER/basicrop/sxhkdrc"
sxhkd -c "/tmp/$USER/basicrop/sxhkdrc" &
sxhkdPID=$!
# somehow this sxhkd trick works on wayland(hyprland)??
}
cropmath(){
eval $cropString
eval "$(gm identify -format sourceWidth=%w\ sourceHeight=%h "$pointerFile")" # variables for dimensions of source image
aspectScreen=$(echo "scale=16; $screenW/$screenH" | bc) # find aspect ratios
aspectFile=$(echo "scale=16; $sourceWidth/$sourceHeight"| bc)
boolWider=$(echo "$aspectFile>$aspectScreen" | bc)
if [ "$boolWider" -eq 1 ]; then
# trim the tops of the crop
cropTrim=$(echo "scale=0; ($(echo "scale=16; ($screenH-($screenW/$aspectFile))/2" | bc)+0.5)/1" | bc)
scale=$(echo "scale=16; $sourceWidth/$screenW" | bc)
caseIn="tops"
elif [ "$boolWider" -eq 0 ]; then
# trim the sides of the crop
cropTrim=$(echo "scale=0; ($(echo "scale=16; ($screenW-($screenH*$aspectFile))/2" | bc)+0.5)/1" | bc)
scale=$(echo "scale=16; $sourceHeight/$screenH" | bc)
caseIn="sides"
fi
# echo $cropTrim
# echo $cropPosX $cropPosY $cropWidth $cropHeight
case $caseIn in #trims the crop if it goes out of bounds
sides)
if [ "$cropPosX" -lt "$cropTrim" ]; then
cropWidth=$((cropWidth - (cropTrim - cropPosX)))
cropPosX=$cropTrim
fi
if [ $((cropPosX + cropWidth)) -gt $((screenW - cropTrim)) ]; then
cropWidth=$((cropWidth - ((cropPosX + cropWidth) - (screenW - cropTrim))))
fi
# echo $cropPosX $cropWidth
;;
tops)
if [ "$cropPosY" -lt "$cropTrim" ]; then
cropHeight=$((cropHeight - (cropTrim - cropPosY)))
cropPosY=$cropTrim
fi
if [ $((cropPosY + cropHeight)) -gt $((screenH - cropTrim)) ]; then
cropHeight=$((cropHeight - ((cropPosY + cropHeight) - (screenH - cropTrim))))
fi
# echo $cropPosY $cropHeight
;;
esac
# Now that the crop dimensions have been cleaned up, they must be downscaled to
# work with the smaller source image. This will also support upscaling.
# echo $scale
# echo $screenW $screenH $sourceWidth $sourceHeight
# echo $cropWidth $cropHeight $cropPosX $cropPosY
case $caseIn in
sides)
# In order for the big-screen measurements from hacksaw to work with
# a real arbitrarily sized image, $cropTrim has to be subtracted from
# the position measurement of whatever dimension (x or y) is getting
# trimmed, because that dimension won't be constrained to the borders
# of the screen.
cropPosX=$(echo "scale=0; ($(echo "scale=16; ($cropPosX-$cropTrim)*$scale" | bc)+0.5)/1" | bc)
cropPosY=$(echo "scale=0; ($(echo "scale=16; ($cropPosY)*$scale" | bc)+0.5)/1" | bc)
;;
tops)
cropPosX=$(echo "scale=0; ($(echo "scale=16; ($cropPosX)*$scale" | bc)+0.5)/1" | bc)
cropPosY=$(echo "scale=0; ($(echo "scale=16; ($cropPosY-$cropTrim)*$scale" | bc)+0.5)/1" | bc)
;;
esac
cropWidth=$(echo "scale=0; ($(echo "scale=16; $cropWidth*$scale" | bc)+0.5)/1" | bc)
cropHeight=$(echo "scale=0; ($(echo "scale=16; $cropHeight*$scale" | bc)+0.5)/1" | bc)
# echo $cropWidth $cropHeight $cropPosX $cropPosY
}
keylogger(){
keylogFile=/tmp/$USER/basicrop/keylog
touch "$keylogFile"
while true; do
lastline="$(tail -n 1 "$keylogFile")"
if [ "$lastline" != "" ]; then
keyInput=$lastline
break
fi
done
}
editoptions(){
case $keyInput in
crop)
echo cropping
if [ "$WAYLAND_DISPLAY" ]; then
cropString=$(slurp -f cropPosX=%x' 'cropPosY=%y' 'cropWidth=%w' 'cropHeight=%h)
else
cropString=$(hacksaw -f cropPosX=%x' 'cropPosY=%y' 'cropWidth=%w' 'cropHeight=%h)
fi
if [ $? -ne 0 ]; then
keyInput="cancel"
editoptions
fi
;;
undo)
echo undoing
kill $imgPID
unset cropString
;;
save)
if [ $i -eq 0 ]; then
echo 'same file. overwriting...'
keyInput="overwrite"
editoptions
else
notify-send -t 2000 "Saving" &
mv "$pointerFile" "$OUTPUT"
kill $imgPID
rm "/tmp/$USER/basicrop/"*
fi
unset cropString
exitBool=true
;;
cancel)
notify-send -t 2000 "Cancelling" &
kill $imgPID
rm "/tmp/$USER/basicrop/"*
unset cropString
exitBool=true
exitCode=1
;;
overwrite)
notify-send -t 2000 "Overwriting" &
kill $imgPID
mv "$pointerFile" "$INPUT"
rm "/tmp/$USER/basicrop/"*
unset cropString
exitBool=true
;;
*)
echo error.
;;
esac
}
# fullscreen image in feh, using hacksaw to crop. Then
# store that image in cache, open in feh, and allow
# user to undo, crop more, or save.
edit() {
pointerFile=$INPUT
i=0
until [ "$exitBool" ]; do
if [ "$keyInput" = "undo" ]; then
if [ $i -le 0 ]; then
pointerFile=$INPUT
elif [ $i -eq 1 ]; then
pointerFile=$INPUT
i=$((i - 1))
else
i=$((i - 1))
rm "/tmp/$USER/basicrop/$i"
pointerFile="/tmp/$USER/basicrop/$((i - 1))"
fi
fi
# echo "i: $i"
# echo "pointerFile: $pointerFile"
imglauncher "$pointerFile"
unset keyInput
keylogger # sets a variable
kill $sxhkdPID
rm "$keylogFile"
editoptions # accepts a variable
if [ -n "$cropString" ]; then
cropmath #big function btw
# crop through graphicsmagick
tempFile="/tmp/$USER/basicrop/$i"
mkdir -p "/tmp/$USER/basicrop/"
gm convert "$pointerFile" -crop "$cropWidth"x"$cropHeight"+"$cropPosX"+"$cropPosY" "$tempFile"
kill $imgPID
pointerFile=$tempFile
i=$((i + 1))
fi
done
}
echo "$INPUT"
echo "$OUTPUT"
notify-send -t 2000 "Editing" &
edit
exit $exitCode