forked from clementfarabet/graphicsmagick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.lua
84 lines (72 loc) · 1.9 KB
/
info.lua
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
-- Dependencies:
require 'sys'
-- Detect/find GM:
local found = sys.fexecute('which identify'):find('identify')
-- Exif parser
local parseExif = require 'graphicsmagick.exif'
-- Which util:
local util
if found then
util = 'identify '
else
return nil
end
-- Helper
local function readarg(file, arg)
local cmd = util .. ' -format ' .. arg .. ' "' .. file .. '"'
local p = io.popen(cmd)
local res = p:read('*all'):gsub('%s*$','')
p:close()
return res
end
-- Command line info:
local function info(path,simple,extexif)
-- parse geometry
local format = readarg(path,'"%m %w %h"')
if format == '' or format:find('^PDF') then
return {
error = 'not an image'
}
end
local format,width,height = format:gfind('(%w*)%s(%d*)%s(%d*)')()
-- simple?
if simple then
return {
width = tonumber(width),
height = tonumber(height),
format = format
}
end
-- parse Exif Data
local exif = readarg(path,'%[exif:*]')
local formatted = (exif..'\n'):gsub('exif:(.-)=(.-)\n', '["%1"] = [========[%2]========],\n ')
local ok,exif = pcall(loadstring( 'return {'..formatted..'}' ))
if not ok then exif = {} end
-- use externally supplied exif to override some variables
if extexif then
for k,v in pairs(extexif) do
exif[k] = v
end
end
-- parse:
local date,location = parseExif(exif)
-- return info
return {
width = tonumber(width),
height = tonumber(height),
format = format,
exif = exif,
date = date,
location = location,
moreinfo = function(format)
if not format then
print('please provide a format string [http://www.imagemagick.org/script/escape.php]')
print('example: moreinfo("%m") will return the format')
return
end
return readarg(path,format)
end
}
end
-- Exports:
return info