Plugin to resize an image
[geeqie.git] / plugins / resize-image / geeqie-resize-image
1 #!/bin/sh
2
3 ## @file
4 ## @brief Resize an image to a specified value.
5 ##
6 ## Requires:
7 ## yad
8 ## ImageMagick
9 ##
10
11 current_dir=$(dirname "$(readlink -f "$0")")
12
13 if ! command -v yad > /dev/null
14 then
15         zenity --window-icon=/usr/local/share/pixmaps/geeqie.png --width=400 --info --title "Geeqie Resize" --text="yad and ImageMagick are required\n\nyad is not installed"
16         exit 0
17 fi
18
19 if ! command -v convert > /dev/null
20 then
21         zenity --window-icon=/usr/local/share/pixmaps/geeqie.png --width=400 --info --title "Geeqie Resize" --text="yad and ImageMagick are required\n\nImageMagick is not installed"
22         exit 0
23 fi
24
25 if [ -z "$1" ]
26 then
27         yad --window-icon=/usr/local/share/pixmaps/geeqie.png --geometry=400 --image dialog-warning --title "Geeqie Resize" --button=gtk-ok:0 --text "\nNo input file was given."
28         exit 0
29 fi
30
31 basefile=$(basename "$1")
32 base=${basefile%.*}
33 ext=${basefile#*.}
34
35 default_filename=$(printf %s "/tmp/$base-resized.$ext")
36 if [ -f "$default_filename" ]
37 then
38         i=1
39         while true
40         do
41                 default_filename=$(printf %s%d%s "/tmp/$base-resized-" "$i" ".$ext")
42                 if [ -f "$default_filename" ]
43                 then
44                         i=$(( i + 1 ))
45                 else
46                         break
47                 fi
48         done
49 fi
50
51
52 selection=$(yad --window-icon=/usr/local/share/pixmaps/geeqie.png \
53 --center                                                          \
54 --title="Resize $1"                                               \
55 --button=gtk-help:"$current_dir"/resize-help.sh                   \
56 --button=gtk-cancel:1                                             \
57 --button=gtk-ok:0                                                 \
58 --text="<b>Reduce image size:</b>\n"                              \
59 --form                                                            \
60 --field="Required size kB":NUM           '100!1..100000!1!'       \
61 --field="Tolerance %":NUM                '10!1..100!1!'           \
62 --field="Max. iterations":NUM            '20!1..100!1!'           \
63 --field="Copy if unchanged":CHK          'FALSE'                  \
64 --field="Strip metadata":CHK             'TRUE'                   \
65 --field="Show computation":CHK           'FALSE'                  \
66 --field="Open output file in Geeqie":CHK 'FALSE'                  \
67 --field="":LBL                                                    \
68 --field="Output file":SFL                                         \
69 --field="Default: $default_filename":LBL )
70
71 if [ -z "$selection" ]
72 then
73         exit 0
74 fi
75
76 size=$(printf %s "$selection" | cut --delimiter="|" --fields=1 -)
77 tolerance=$(printf %s "$selection" | cut --delimiter="|" --fields=2 -)
78 iterations=$(printf %s "$selection" | cut --delimiter="|" --fields=3 -)
79 copy_unchanged=$(printf %s "$selection" | cut --delimiter="|" --fields=4 -)
80 strip_metadata=$(printf %s "$selection" | cut --delimiter="|" --fields=5 -)
81 show_computation=$(printf %s "$selection" | cut --delimiter="|" --fields=6 -)
82 open_geeqie=$(printf %s "$selection" | cut --delimiter="|" --fields=7 -)
83 file=$(printf %s "$selection" | cut --delimiter="|" --fields=9 -)
84
85 if [ -z "$file" ]
86 then
87         new_filename="$default_filename"
88 else
89         if [ -f "$file" ]
90         then
91                 if ! yad --window-icon=/usr/local/share/pixmaps/geeqie.png --geometry=300 --image dialog-warning --title "Geeqie Resize" --text "\nOutput file already exists.\nOverwrite?"
92                 then
93                         exit 0
94                 fi
95         fi
96         new_filename="$file"
97 fi
98
99 if [ "$copy_unchanged" = "FALSE" ]
100 then
101         copy="n"
102 else
103         copy="y"
104 fi
105
106 if [ "$strip_metadata" = "FALSE" ]
107 then
108         strip="n"
109 else
110         strip="y"
111 fi
112
113 tmp_file=$(mktemp "${TMPDIR:-/tmp}/geeqie.XXXXXXXXXX")
114
115 yad --window-icon=/usr/local/share/pixmaps/geeqie.png --geometry=300 --image dialog-information --title "Geeqie Resize" --button=OK:0 --text "Running Downsize...." &
116 info_id="$!"
117
118 result=$("$current_dir"/downsize -s "$size" -t "$tolerance" -m "$iterations" -c "$copy" -S "$strip" "$1" "$new_filename" > "$tmp_file" 2>/dev/null)
119
120 if [ "$?" -eq 1 ]
121 then
122         kill "$info_id"
123         rm "$tmp_file"
124
125         yad --window-icon=/usr/local/share/pixmaps/geeqie.png --geometry=400 --image dialog-warning --title "Geeqie Resize" --button=gtk-ok:0 --text "Downsize failed.\n\nIf the filetype is not supported by Downsize,\ntry the Export plugin to get a jpeg.\n\nDownsize error message:\n$result"
126         exit 0
127 fi
128
129 kill "$info_id"
130
131 if [ "$open_geeqie" = "TRUE" ]
132 then
133         geeqie --remote "$new_filename"
134 fi
135
136
137 if [ "$show_computation" = "TRUE" ]
138 then
139         yad --window-icon=/usr/local/share/pixmaps/geeqie.png --title "Geeqie Resize computation" --text="$new_filename\n\n$(cat "$tmp_file")" --button=gtk-ok:0
140 fi
141
142 rm "$tmp_file"
143
144 exit 0
145