Open Source Image Processing

The Idea

Image processing has always fascinated me. Especially when it's used in Neural Networks.
After coding a very basic module in NodeJS to manipulate images,
I decided to try and implement this in Lua so it could be used on Roblox.

Showcase

An example of a GIF converted to images playing in Roblox.

Image Kernels

Compositing

Rotation (Currently only 90 and 180 degrees)

Flipping Along the X- and Y-axis

Inverting

Usage

local Image = require(script.Image)

local brightnessArray = {}
math.randomseed(tick())
for i = 1, 100 do
	for j = 1, 100 do
		table.insert(brightnessArray, math.max(0, (math.noise(i/100, j/100) + 0.5) * 255))
	end
end

local img = Image.fromMonochrome(100, 100, brightnessArray)
for i = 1, img.height do
	for j = 1, img.width do
		local index = (i - 1) * img.width + j
		local pixel = img.pixels[index]
		
		local p = Instance.new("Part")
		p.Anchored = true
		p.Size = Vector3.new(1, 1, 1)
		p.CFrame = CFrame.new(i, 10, j)
		p.Color = Color3.fromRGB(pixel, pixel, pixel)
		p.Material = Enum.Material.SmoothPlastic
		p.Parent = workspace
	end
	wait()
end

--[[
These functions can be used to manipulate the image:
- applyKernel(kernel: table)
- cannyEdges()
- composite(im: Image, x: number, y: number, alpha: number)
- edges()
- flipX() and flipY()
- gaussianBlur()
- getPixel(x: number, y: number)
- grayscale()
- horizontalLines()
- invert()
- rotate90() and rotate180()
- setAlpha(a: number)
- setPixel(x: number, y: number, color: table)
- sharpen() and unsharpen()
- verticalLines()
]]--

Download

https://www.roblox.com/library/4766927144/Image-Processing-Library

I hope you can find great use to this, let me know what functions you would like me to add.
I would love to see what you have made using this module, reply below!

67 Likes

So this is like the video player but a library?

No not really, you can play videos when you update the frame with multiple (stored) pixel arrays, but that’s just one of the possibilities where you could use this for.
I mainly made it for this purpose:

Because I was just wondering if it was possible to be made in Roblox.
You can use this module to render raycasting, visualize perlin noise and more.

3 Likes

I am confused on how you use this, can I have source of the gif example? :smiley:

2 Likes

This looks great! I may use it later on when I am going to be needing it!

2 Likes

well i mean like a gif in my world is a video

1 Like

I am not allowed to show you how to load an image from any url.
But I can give you an example of how to play videos using multiple pixel arrays.

local Image = require(script.Image)

local remote = game:GetService("ReplicatedStorage").GetImage
local background = script.Parent:WaitForChild("ScreenGui"):WaitForChild("Background")

local images = {}
local imageData = table.create(120, {
	width = 20,
	height = 20,
	pixels = table.create(400, {50, 50, 50})
})

for i = 1, #imageData do
	local data = imageData[i]
	local newImage = Image.new(data.width, data.height, data.pixels, 3)
	newImage:setAlpha(0)
	newImage:setCanvas(background)
	newImage:setPixel(math.floor(math.random() * data.width + 1), math.floor(math.random() * data.height + 1), {255, 100, 255})
	table.insert(images, newImage)
	wait()
end

local copy = images[1]:copy()
copy:setCanvas(background)
copy:create()
while true do
	for i = 1, #images do
		copy.pixels = images[i].pixels -- Fastest method to composite images with the same dimensions.
		--copy:composite(images[i], 0, 0, 0) -- When you are working with different sized images, you can use this instead of directly setting the pixels.
		copy:update() -- Updates the frames with the corresponding pixels.
		wait() -- Delay between frames
	end
end

You could also use this loop for more frames per second, you won’t have any control of the speed though.

local i = 1
local frameCount = #images
game:GetService("RunService").RenderStepped:Connect(function()
    copy.pixels = images[i].pixels
    copy:update()
    i = i % frameCount + 1
end)

EDIT:
Removed the frame functionality, so the code above doesn’t work anymore.
This was removed so the code could be used in other things besides just frames.

4 Likes

Why is that? Is it against TOS or something?

1 Like

Yea, bypassing Roblox’s image moderation breaks TOS.
It’d be bad if skids went around uploading NSFW pictures to a game who’s demographic includes underaged children.

4 Likes

I wonder if its a good idea to use richtext for the pixel rendering or instance.new a textLabel or Frame.

How can i use this module to render art? or an image or GIF? the previous code you made, you said that that one doesnt work…

You don’t need to show me how to get the pixels of an image and all those things (yk the things you didnt want to show cuz of TOS).

I’m not gonna help you break the Roblox terms of service, sorry mate.

That’s fine, i already had an alternative lol.

don’t necro bump a 2 year old post once, then again after a month.

3 Likes