Skip to content

Instantly share code, notes, and snippets.

@Yonaba
Created June 29, 2011 14:34
Show Gist options
  • Save Yonaba/1053948 to your computer and use it in GitHub Desktop.
Save Yonaba/1053948 to your computer and use it in GitHub Desktop.
Image Processing Using Lua
-- A Library that provides naive-implemented algorithms for image processing
-- All functions assumes 'img' is a reference to a Drawable object loaded onto memory.
--Flips vertically a picture
function getImageFlipVertical(img)
local w = img:width()-1
local h = img:height()-1
local flip = Image.createEmpty(w+1,h+1)
for y = h,0,-1 do
for x = 0,w,1 do
local col = img:pixel(x,y)
flip:pixel(x,h-y,col)
end
end
return flip
end
-- Rotates a picture
-- BackColor is Optional
function Rotate(img,angle,backColor)
local angRad = angle*math.pi/180
local cosine = math.cos(angRad)
local sine = math.sin(angRad)
local width_out = math.ceil(img:width()*math.abs(cosine)+img:height()*math.abs(sine))
local height_out = math.ceil(img:width()*math.abs(sine)+img:height()*math.abs(cosine))
local out = img.createEmpty(width_out,height_out)
out:clear(backColor or Color.new(255,255,255))
local x_center_image = img:width()/2
local y_center_image = img:height()/2
local x_center_outImage = out:width()/2
local y_center_outImage = out:height()/2
for y=0,height_out-1 do
for x = 0,width_out-1 do
local outX = math.ceil(cosine*(x-x_center_outImage)+sine*(y-y_center_outImage)+x_center_image)
local outY = math.ceil(-sine*(x-x_center_outImage)+cosine*(y-y_center_outImage)+y_center_image)
if outX >= 0 and outX < img:width() and outY >=0 and outY < img:height() then
out:pixel(x,y,img:pixel(outX,outY))
end
end
end
return out
end
-- Resizes a picture
function Resize(img,newX,newY)
local calc = img.createEmpty(newX, newY)
local w, h = img:width(), img:height()
for x = 1, newX do
for y = 1, newY do
local intX = math.floor(x*(w/newX))
local intY = math.floor(y*(h/newY))
calc:pixel(x,y,img:pixel(intX,intY))
end
end
return calc
end
-- Negates a picture
function nvgImage(img)
local im = img.createEmpty(img:width(),img:height())
for y =0,img:height()-1 do
for x = 0,img:width()-1 do
local color = img:pixel(x,y)
local r = color:colors().r
local g = color:colors().g
local b = color:colors().b
local a = color:colors().a
local nwCol = Color.new(255-r,255-g,255-b,a)
im:pixel(x,y,nwCol)
end
end
return im
end
-- Flashes a picture
function flashImg(img)
local canalMin = {["r"]=255,["g"]=255,["b"]=255,["a"]=255}
local canalMax = {["r"]=0,["g"]=0,["b"]=0,["a"]=0}
local w,h = img:width(),img:height()
local out = img.createEmpty(w,h)
for y=0,h-1 do
for x=0,w-1 do
local colSet = img:pixel(x,y):colors()
for k in pairs(colSet) do
if colSet[k]<canalMin[k] then canalMin[k]=colSet[k] end
if colSet[k]>canalMax[k] then canalMax[k]=colSet[k] end
end
end
end
for k,v in pairs(canalMin) do print('min',k,v) end
for k,v in pairs(canalMax) do print('max',k,v) end
for y=0,h-1 do
for x=0,w-1 do
local colSet = img:pixel(x,y):colors()
for k in pairs(colSet) do
colSet[k] = ((colSet[k]-canalMin[k])*255)
colSet[k]= colSet[k]/(canalMax[k]-canalMin[k])
end
local nwCol = Color.new(colSet.r,colSet.g,colSet.b,img:pixel(x,y):colors().a)
out:pixel(x,y,nwCol)
end
end
return out
end
@elvenhope
Copy link

How Do you use it?

Do I just do like

local ImagePR = require("improcessing.lua")

and then

ImageRP:flashImg(IMG)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment