The question

While I was browsing through the many image manipulating algorithms beknownst to us it came to a surprise that I couldn’t find a programming language specific to this purpose.

 

There are a great many algorithms out there. Almost every popular programming language has a huge amount of graphical subroutines that allow manipulation.Don’t feel that’s a waste? Every platform needs to implement the same routines over and over again…This means different APIs, different names, etc…

Wouldn’t it be great to have one language instead, that allows programmers across different platforms to work on images uniformely? Much like SQL does?

How far do we get with SQL anyway?

The experiment

What if we stored images in a database. Not as a BLOB but as a bunch of tuples (x,y,r,g,b,a) . How good or bad a candidate is SQL?

I’ve created some code to load images into a MySQL database and load it back to a file again. This allows us to adjust the images just by executing SQL statements.

You can find the code here:

github repo here.

This however is limited to simple things like adjusting the brightness.

Here is the stock image I’ve used:

jpeg example.jpg

If we apply the following oneliner:

— greyscale of the image by taking the least value.

INSERT INTO imagedata(imageid, x, y, r, g, b, a) SELECT 4 as imageid, x, y, least(r,g,b) as r, least(r,g,b) as g, least(r,g,b) as b , least(r,g,b) as a from imagedata where imageid=3;

This yields:

jpeg example.least grey.jpg

 

Conclusion

While doing simple transformation are simple and somewhat clean.

Doing a gaussian blur however seems to be more complicated. This is mainly because blurs use deviations and other devious statistical tricks. But is it impossible? Are you up to the challenge to do a blur in SQL? Let me know!

Update

This is the code for a horrible gaussian blur of 2 pixels wide.

INSERT INTO imagedata(x,y, imageid, r,g,b,a)
SELECT x, y, 4 as imageid,
floor(((SELECT
r
FROM
imagedb.imagedata
WHERE
x = source.x AND y = source.y
AND imageid = source.imageid) * 6 + (SELECT
SUM(r)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4) / (6 + (SELECT
COUNT(r)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4)) AS r,
floor(((SELECT
g
FROM
imagedb.imagedata
WHERE
x = source.x AND y = source.y
AND imageid = source.imageid) * 6 + (SELECT
SUM(g)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4) / (6 + (SELECT
COUNT(g)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4)) AS g,
floor(((SELECT
b
FROM
imagedb.imagedata
WHERE
x = source.x AND y = source.y
AND imageid = source.imageid) * 6 + (SELECT
SUM(b)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4) / (6 + (SELECT
COUNT(b)
FROM
imagedb.imagedata
WHERE
x – source.x = 1 AND y – source.y = 1
AND imageid = source.imageid) * 4)) AS b,
(SELECT
a
FROM
imagedb.imagedata
WHERE
x = source.x AND y = source.y
AND imageid = source.imageid) AS a
FROM
imagedb.imagedata AS source
WHERE
imageid = 3;

 

This is a horrible query and it takes more than an hour to execute but it works!