-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtile_images.lua
117 lines (98 loc) · 3.75 KB
/
tile_images.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
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
-----------------------------------------------------
-- ----------------------------------------------- --
-- ▄ ▄ ▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄ ▄ --
-- ▐░▌ ▐░▌ ▐░▌▐░█▀▀▀▀▀ ▀▀█░█▀▀ ▐░▌ ▐░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░█ █░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▐░░░░░░░▌ --
-- ▐░▌ ▐░▌ ▐░▌▐░▌ ▐░▌ ▀▀▀▀▀█░▌ --
-- ▐░█▄▄▄▄▄ ▐░█▄▄▄█░▌▐░█▄▄▄▄▄ ▄▄█░█▄▄ ▐░▌ --
-- ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀▀▀▀▀▀▀ ▀ --
-- ----------------------------------------------- --
-- ---- Helper script to tile ampache images ----- --
-- -------- https://github.com/icefields --------- --
-----------------------------------------------------
local socket = require("socket.http")
local ltn12 = require("ltn12")
local lfs = require("lfs")
-- Function to download image from URL and save it locally
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
-- Function to split a string by a given delimiter
local function split(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
-- function to tile the images from the urls parameter in a single image
-- commaSeparated boolean that indicates if the urls are a single comma
-- separated string or an array of strings
local function tileImages(urlList, commaSeparated)
local urls
if (commaSeparated) then
urls = split(urlList, ",")
elseif type(myVariable) == "table" then
urls = urlList
end
-- Create a directory to store downloaded images
local img_dir = "images"
if not lfs.attributes(img_dir, "mode") then
lfs.mkdir(img_dir)
end
-- Check if at least one URL is provided
if #urls == 0 then
error("No URLs provided. Usage: lua tile_images.lua <url1> <url2> ... <urlN>")
end
-- Determine the number of images
local num_images = #urls
-- Calculate the dimensions of the grid
local grid_size = math.ceil(math.sqrt(num_images))
local num_rows = grid_size
local num_cols = grid_size
-- Download images
for i, url in ipairs(urls) do
local filename = img_dir .. "/image" .. i .. ".jpg"
download_image(url, filename)
end
-- Prepare the ImageMagick montage command
local command = "montage"
for i = 1, num_images do
local filename = img_dir .. "/image" .. i .. ".jpg"
command = command .. " " .. filename
end
command = command .. " -tile " .. num_cols .. "x" .. num_rows .. " -geometry +0+0 tiled_image.jpg"
-- Execute the command to tile images
os.execute(command)
-- Remove temporary image files
for i = 1, num_images do
local filename = img_dir .. "/image" .. i .. ".jpg"
os.remove(filename)
end
end
-- Parse command line arguments
if arg[1] == "-t" then
-- Get URLs from command line arguments
tileImages(arg[2], true)
end
return {
tileImages = tileImages
}