-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtile_images.lua
More file actions
110 lines (89 loc) · 3.09 KB
/
tile_images.lua
File metadata and controls
110 lines (89 loc) · 3.09 KB
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
-----------------------------------------------------
-- ----------------------------------------------- --
-- ▄ ▄ ▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄ ▄ --
-- ▐░▌ ▐░▌ ▐░▌▐░█▀▀▀▀▀ ▀▀█░█▀▀ ▐░▌ ▐░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█ █░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░░░░░░░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▀▀▀▀▀█░▌ --
-- ▐░█▄▄▄▄▄ ▐░█▄▄▄█░▌▐░█▄▄▄▄▄ ▄▄█░█▄▄ ▐░▌ --
-- ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀ --
-- ----------------------------------------------- --
-- ---- Helper script to tile ampache images ----- --
-- -------- https://github.com/icefields --------- --
-----------------------------------------------------
local socket = require("socket.http")
local ltn12 = require("ltn12")
local lfs = require("lfs")
socket.TIMEOUT = 10
socket.redirect = true
local function download_image(url, filename)
local file, err = io.open(filename, "wb")
if not file then
error("Failed to open file: " .. err)
end
local response_body = {}
local _, code = socket.request {
url = url,
sink = ltn12.sink.table(response_body)
}
if code ~= 200 then
file:close()
error("Failed to download " .. url .. ": HTTP code " .. code)
end
file:write(table.concat(response_body))
file:close()
end
local function split(inputstr, sep)
sep = sep or "%s"
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
local function trim(s)
return s:match("^%s*(.-)%s*$")
end
local function tileImages(urlList, commaSeparated)
local urls = {}
if commaSeparated then
for _, u in ipairs(split(urlList, ",")) do
table.insert(urls, trim(u))
end
elseif type(urlList) == "table" then
urls = urlList
else
error("Invalid input")
end
if #urls == 0 then
error("No URLs provided")
end
local img_dir = "images"
if not lfs.attributes(img_dir, "mode") then
lfs.mkdir(img_dir)
end
for i, url in ipairs(urls) do
download_image(url, img_dir .. "/image" .. i .. ".img")
end
local count = #urls
local grid = math.ceil(math.sqrt(count))
local tile = 256
local cmd = "montage"
for i = 1, count do
cmd = cmd .. " " .. img_dir .. "/image" .. i .. ".img"
end
cmd = cmd .. string.format(
" -resize %dx%d^ -gravity center -extent %dx%d -tile %dx%d -geometry +0+0 tiled_image.jpg",
tile, tile, tile, tile, grid, grid
)
os.execute(cmd)
for i = 1, count do
os.remove(img_dir .. "/image" .. i .. ".img")
end
end
if arg[1] == "-t" then
tileImages(arg[2], true)
end
return {
tileImages = tileImages
}