-
Notifications
You must be signed in to change notification settings - Fork 41
/
convert.lua
75 lines (64 loc) · 2.23 KB
/
convert.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
-- Dependencies:
require 'sys'
-- Detect/find GM:
local findgm = sys.uname() == 'windows' and 'where gm' or 'which gm'
local found = sys.fexecute(findgm):find('gm')
if not found then
print 'gm (GraphicsMagick) binary not found, please install (see www.graphicsmagick.org)'
end
-- Command line convert:
local function convert(...)
-- args
local args = dok.unpack(
{...},
'gm.convert',
'Converts an image into another.',
{arg='input', type='string', help='path to input image', req=true},
{arg='output', type='string', help='path to output image', req=true},
{arg='size', type='string', help='destination size'},
{arg='rotate', type='number', help='rotation angle (degrees)'},
{arg='vflip', type='boolean', help='flip image vertically'},
{arg='hflip', type='boolean', help='flip image horizontally'},
{arg='quality', type='number', help='quality (0 to 100)', default=90},
{arg='benchmark', type='boolean', help='benchmark command', default=false},
{arg='verbose', type='boolean', help='verbose', default=false}
)
-- hint input size:
local options = {}
if args.size then
table.insert(options, '-size ' .. args.size)
end
-- input path:
table.insert(options, args.input)
-- unpack commands:
for cmd,val in pairs(args) do
if cmd == 'size' then
table.insert(options, '-resize ' .. val)
elseif cmd == 'rotate' then
table.insert(options, '-rotate ' .. val)
elseif cmd == 'quality' then
table.insert(options, '-quality ' .. val)
elseif cmd == 'verbose' and val then
table.insert(options, '-verbose')
elseif cmd == 'vflip' and val then
table.insert(options, '-flip')
elseif cmd == 'hflip' and val then
table.insert(options, '-flop')
end
end
-- output path:
table.insert(options, args.output)
-- pack command:
local cmd
if args.benchmark then
cmd = 'gm benchmark convert '
else
cmd = 'gm convert '
end
cmd = cmd .. table.concat(options, ' ')
-- exec command:
if args.verbose then print(cmd) end
os.execute(cmd)
end
-- Exports:
return (found and convert)