-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_image_ext.sh
executable file
·42 lines (35 loc) · 1.11 KB
/
get_image_ext.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
#!/usr/bin/env bash
set -euo pipefail
# Prints a normalized extension for a valid image (according to 'file', 'identify', and 'feh').
(( $# == 1 )) || exit 1
m_fulltype=$(file -Lb --mime-type -- "$1")
m_type=${m_fulltype%/*}
m_subtype=${m_fulltype##*/}
[[ $m_type == 'image' ]] || exit 0
case $m_subtype in
x-ms-bmp) newext=bmp ;;
x-exr) newext=exr ;;
jpeg) newext=jpg ;;
x-portable-bitmap) newext=pbm ;;
x-portable-greymap) newext=pgm ;;
png) newext=png ;;
x-portable-pixmap) newext=ppm ;;
tiff) newext=tiff ;;
webp) newext=webp ;;
*) newext='' ;;
esac
[[ -n $newext ]] || exit 0
case $newext in
jpg|png) feh -U -- "$1" >/dev/null || exit 0 ;;
*) identify -format '' -- "$1" >/dev/null || exit 0 ;;
esac
if [[ $newext == 'jpg' ]]; then
# Detect broken/unreadable multi-image JPEGs
python - "$1" <<'EOF' || exit 0
import sys; from PIL import Image
with Image.open(sys.argv[1]) as img:
if getattr(img, 'is_animated', False):
img.seek(img.n_frames - 1)
EOF
fi
printf '%s\n' "$newext"