-
Notifications
You must be signed in to change notification settings - Fork 9
/
identifi
executable file
·163 lines (139 loc) · 4.04 KB
/
identifi
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
#!/bin/sh
#
# This is a simple wrapper for ImageMagick's identify tool.
# It outputs relevant information in an easy-to-understand format.
#
# See also:
# * https://unix.stackexchange.com/questions/406454/how-to-get-the-dpi-of-an-image-file-png
# * https://imagemagick.org/script/identify.php
# * https://imagemagick.org/script/escape.php
# * https://github.com/ImageMagick/ImageMagick/issues/5950
# Using '%[fx:int(resolution.x)]' instead of '%x' to round to an integer.
# Using '%[fx:int(resolution.y)]' instead of '%y' to round to an integer.
# Basic file info:
# %i full filename (%f is just the basename)
# %B file size (bytes)
#
# Basic image info:
# %w width
# %h height
# %m format (JPEG, PNG, GIF)
# %C %[compression] image compression type (JPEG, Zip, LZW)
# %[type] image type (palette, TrueColor)
# %z %[bit-depth] depth (8-bit)
# %[channels] colorspace neumonic followed by total number of channels and meta channels (srgba 5.0)
#
# Optional image info:
# %Q compression quality (useful for JPEG, default=92)
# %[gamma] gamma (default=0.454545)
# %[orientation] orientation
# %x,%y resolution (dpi)
#
# GIF-related:
# %p index of image in current image list (for GIF or other multi-image formats)
# %D image GIF dispose method
# %T image time delay (in centi-seconds)
#
# Not used:
# %k %[colors] number of unique colors (expensive to compute on large images)
# %r %[colorspace] colorspace (sRGB)
# %[printsize.x] print size (inches?)
COLOR=1
case "$1" in
--nocolor|--no-color|--nocolour|--no-colour)
COLOR=0
;;
esac
if [ "$COLOR" = 1 ] ; then
RED="[31m"
BOLD="[1m"
NORMAL="[m"
else
RED=""
BOLD=""
NORMAL=""
fi
for f in "$@" ; do
# `identify` likes to buffer all the output and flush it only upon exit.
# Calling identify for each file shows the results progressively, which is better UX.
#
# Calling it for each file adds some overhead and makes the overall script
# slower for many files. However, by the time this difference is
# significant, the user should probably be calling identify directly (with
# a custom format).
#
# Alternatively, I could have used `stdbuf` or `unbuffer`, but that would
# add one extra dependency on this script, making it less portable.
# https://stackoverflow.com/questions/11337041/force-line-buffering-of-stdout-in-a-pipeline
identify -units PixelsPerInch -format "%B*%w*%h*%m*%C*%[type]*%z*%[channels]*%Q*%[gamma]*%[orientation]*%[fx:int(resolution.x)]*%[fx:int(resolution.y)]*%p*%D*%T*%i\n" "$f"
case "$?" in
0)
# No errors.
;;
2)
# Keyboard interrupt, let's abort.
break
;;
*)
echo "${RED}Error${NORMAL} (exit=$?) identifying $f" > /dev/stderr
esac
done | awk '
BEGIN {
FS="*"
}
{
# For debugging, uncomment this:
# print $0
bytes = $1
width = $2
height = $3
format = $4
compression = $5
type = $6
bitdepth = $7
channels = $8
quality = $9
gamma = $10
orientation = $11
resolutionx = $12
resolutiony = $13
imgindex = $14
gifdispose = $15
timedelay = $16 * 10
filename = $17
pixels = width * height
megapixels = sprintf("%.1f", pixels / 1000000)
out = ""
if (imgindex > 0) {
out = out "⤷ " imgindex " "
}
if (timedelay) {
out = out "[" timedelay "ms " gifdispose "] "
}
out = out format " " type " " bitdepth "-bit " channels " "
if (compression == "JPEG" || quality != 92) {
out = out "(" compression " " quality ") "
} else if (compression != "Undefined") {
out = out "(" compression ") "
}
out = out width "x" height " pixels " megapixels "MP "
if (resolutionx > 0 && resolutiony > 0) {
mm_width = sprintf("%.0f", width * 25.4 / resolutionx)
mm_height = sprintf("%.0f", height * 25.4 / resolutiony)
out = out "(" mm_width "x" mm_height "mm "
if (resolutionx == resolutiony) {
out = out resolutionx "dpi) "
} else {
out = out resolutionx "x" resolutiony "dpi) "
}
}
if (orientation != "Undefined") {
out = out orientation " orientation "
}
if (gamma != 0.454545) {
out = out gamma " gamma "
}
out = out bytes " bytes: " "'"${BOLD}"'" filename "'"${NORMAL}"'"
print out
}
'