summaryrefslogtreecommitdiff
path: root/html/examples/multicrop_gif.lua
blob: 5cf85a06c2f0ca219813ed7d5315effff490754c (plain)
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
-- lua multicrop_gif.lua 60 60 0 0 newfile.gif DSC003*.jpg

require"imlua"
require"imlua_process"

err_msg = {
  "No error.",
  "Error while opening the file.",
  "Error while accessing the file.",
  "Invalid or unrecognized file format.",
  "Invalid or unsupported data.",
  "Invalid or unsupported compression.",
  "Insuficient memory",
  "Interrupted by the counter",
}

-- Margin parameters
x1 = arg[1]
x2 = arg[2]
y1 = arg[3]
y2 = arg[4]
new_filename = arg[5]
filename1 = arg[6]
if (not x1 or not x2 or not y1 or not y2 or not new_filename or not filename1) then
  print("Must have the rectangle coordinates and at least one file name as parameters.")
  print("  Can have more than one file name as parameters and can use wildcards.")
  print("  Usage:")
  print("    lua multicrop_gif.lua x1 x2 y1 y2 new_filename filename1 filename2 ...")
  return
end

print(">>> Crop of multiple images <<<")

function ProcessImageFile(file_name, ifile)
  print("Loading File: "..file_name)
  local image, err = im.FileImageLoad(file_name);
  if (err and err ~= im.ERR_NONE) then
    error(err_msg[err+1])
  end

  local new_image = im.ProcessCropNew(image, x1, image:Width()-1-x2, y1, image:Height()-1-y2)
  local map_image = im.ImageCreateBased(new_image, nil, nil, im.MAP, im.BYTE)
  im.ConvertColorSpace(new_image, map_image)
  ifile:SaveImage(map_image)

  map_image:Destroy()
  new_image:Destroy()
  image:Destroy()
end

ifile = im.FileNew(new_filename, "GIF")

ifile:SetAttribute("Delay", im.USHORT, {30}) -- Time to wait betweed frames in 1/100 of a second.
ifile:SetAttribute("Iterations", im.USHORT, {0}) -- The number of times to repeat the animation. 0 means to repeat forever.

file_count = 0
for index,value in ipairs(arg) do
  if (index > 5) then
    ProcessImageFile(arg[index], ifile)
    file_count = file_count + 1
  end
end

ifile:Close()

if (file_count > 1) then
  print("Processed "..file_count.." Files.")
end
print("Saved File: "..new_filename)