function tablelength(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end w, h, g = img.width, img.height, img.getInt x1, y1, x2, y2 = 0, 0, w, h -- left side colors = {} while x1 < x2 do for y=y1, y2-1 do colors[g(x1, y)] = 1 end if tablelength(colors) > 1 then break end x1=x1+1 end -- top side colors = {} while y1 < y2 do for x=x1, x2-1 do colors[g(x, y1)] = 1 end if tablelength(colors) > 1 then break end y1=y1+1 end -- right side colors = {} while x2 > x1 do for y=y1, y2-1 do colors[g(x2-1, y)] = 1 end if tablelength(colors) > 1 then break end x2=x2-1 end -- bottom side colors = {} while y2 > y1 do for x=x1, x2-1 do colors[g(x, y2-1)] = 1 end if tablelength(colors) > 1 then break end y2=y2-1 end if x2 <= x1 or y2 <= y1 then -- weird result? return full image x1, y1, x2, y2 = 0, 0, w, h end return "Crop: "..x1..", "..y1..", "..x2..", "..y2