LuaImg

Script Language Examples

Lua programs are also LuaImg programs, so the Lua hello world program stands:

print "Hello world!"

Create a 32x32 image with 3 channels containing solid red (vec(1,0,0)). Save to a file (can also be written on one line):

LuaImg output
my_img = make(vec(32,32), 3, vec(1,0,0))
my_img:save("red.png")

Draw a black border around that image and a few white pixels in it.

LuaImg output
my_img:drawLine(vec(0,0),vec(0,31),1,vec(0,0,0))
my_img:drawLine(vec(0,0),vec(31,0),1,vec(0,0,0))
my_img:drawLine(vec(31,31),vec(0,31),1,vec(0,0,0))
my_img:drawLine(vec(31,31),vec(31,0),1,vec(0,0,0))
my_img:draw(vec(10,15),vec(1,1,1))
my_img:draw(vec(15,15),vec(1,1,1))
my_img:draw(vec(15,10),vec(1,1,1))
my_img:save("redb.png")

Initialising a 3 channel + alpha image using a function (computes an antialiased blue circle with alpha):

LuaImg output
local sz = vec(64,64)
function init(pos)
    local rad = #(pos - sz/2);
    local alpha = clamp(30-rad, 01)
    return vec(001, alpha)
end
circle = make(sz, 3true, init)
circle:save("circle.png")

From the circle image, rotate the colour channels (preserving the alpha), and blend onto a solid yellow background, then enlarge it to get a black border:

LuaImg output
img = (circle.zxyW .. vec(1,1,0)):crop(vec(-1,-1), vec(66,66), 0)
img:save("circle_bg.png")

Blend the circle image onto red.png:

LuaImg output
smaller = circle:scale(vec(32,32),"BICUBIC")
blended = smaller .. open("red.png")
blended:save("circle_bg_red.png")

Extract alpha channel and save it:

LuaImg output
my_mask = circle.w
my_mask:save("circle_a.png")

Random RGB noise:

LuaImg output
function randvec() return vec(random(), random(), random()) end
my_noise = make(vec(64,64), 3, randvec)
my_noise:save("random.png")

Random noise with gaussian blur. The gaussian(n) function returns an nx1 image that represents a separated normalised gaussian blur kernel. One can also use custom kernels by providing an image instead of using the result of gaussian() -- and these can be provided either in separated form, or as a general rectangular matrix.

LuaImg output
my_perlin = my_noise:convolveSep(gaussian(7), truetrue)
my_perlin:save("perlin.png")

Subtract the two to create a high frequency noise texture. Generally, using arithmetic to combine images is supported.

LuaImg output
((my_noise-my_perlin) + 0.5):save("noise_hifreq.png")

Valid XHTML 1.0 Strict