Plugin for image crop
[geeqie.git] / plugins / image-crop / geeqie-image-crop
1 #!/bin/bash
2
3 # Crop image
4 #
5 # Requires ImageMagick and exiftool
6
7
8 process_raw ()
9 {
10         tmpdir=$(mktemp --tmpdir --directory geeqie_crop_image_XXXXXX)
11
12         list=$(exiv2 -pp "$1")
13         if [[ ! -z "$list" ]]
14         then
15                 readarray -t split_list <<<"$list"
16
17                 array_length="${#split_list[@]}" 
18                 exiv2 -ep"$array_length" "$1" --location "$tmpdir"
19
20                 src_filename=$(ls "$tmpdir/")
21                 filename="${src_filename%.*}"
22                 extension="${src_filename##*.}"
23                 rotation=$(exiftool -Orientation -n "$1" | cut -d':' -f2 | xargs)
24                 convert "$tmpdir/$src_filename" -crop "$2" "$tmpdir/$filename-crop.$extension"
25
26                 exiftool -Orientation=$rotation -n "$tmpdir/$filename-crop.$extension"
27
28                 rm "$tmpdir/$src_filename"
29
30                 geeqie --remote view:"$tmpdir/$filename-crop.$extension"
31                 res=0
32         else
33                 res=1
34         fi
35
36         return $res
37 }
38
39 process_plain ()
40 {
41         tmpdir=$(mktemp --tmpdir --directory geeqie_crop_image_XXXXXX)
42
43         src_filename=$(basename -- "$1")
44         filename="${src_filename%.*}"
45         extension="${src_filename##*.}"
46         convert "$1" -crop "$2" "$tmpdir/$filename-crop.$extension"
47         if [ $? = 1 ]
48         then
49                 zenity --error --title="$title" --text="Cannot process this file format" --width="$width" --window-icon="$window_icon"
50         else
51                 geeqie --remote view:"$tmpdir/$filename-crop.$extension"
52         fi
53 }
54
55 export window_icon="/usr/local/share/pixmaps/geeqie.png"
56 export title="Geeqie crop image"
57 export width="250"
58
59 if [ -x "$(command -v convert)" ]
60 then
61         if [ -x "$(command -v exiftool)" ]
62         then
63
64                 coords=$(geeqie --remote --get-rectangle)
65
66                 if [ -z "$coords" ]
67                 then
68                         zenity --error --title="$title" --text="Rectangle coordinates have not been set" --width="$width" --window-icon="$window_icon" 2>/dev/null
69                         exit 0
70                 fi
71
72                 filename=$(basename -- "$1")
73                 extension="${filename##*.}"
74
75                 if [ "${extension,,}" = "jpeg" ]
76                 then
77                         source_file="$1"
78                         process_plain "$1" $coords
79                 elif [ "${extension,,}" = "jpg" ]
80                 then
81                         source_file="$1"
82                         process_plain "$1" $coords
83                 elif [ "${extension,,}" = "png" ]
84                 then
85                         source_file="$1"
86                         process_plain "$1" $coords
87                 elif [ "${extension,,}" = "tif" ]
88                 then
89                         source_file="$1"
90                         process_plain "$1" $coords
91                 elif [ "${extension,,}" = "tiff" ]
92                 then
93                         source_file="$1"
94                         process_plain "$1" $coords
95                 else
96                         process_raw "$1" $coords
97                         if [ $? = 1 ]
98                         then
99                                 process_plain "$1" $coords
100                         fi
101                 fi
102         else
103                 zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nexiftool is not installed" --title="$title" --window-icon="$window_icon" 2>/dev/null
104                 exit 0
105         fi
106 else
107         zenity --info --title="$title" --width="$width" --height=100 --text="Crop image\n\nImageMagick is not installed" --title="$title" --window-icon="$window_icon" 2>/dev/null
108         exit 0
109 fi