Minimalistic Clojure Library for Work with Images

I don’t have a big expirience with Clojure. Some time ago, I decided to create tiny renderer into Clojure. To that end, I create very simple library for working with image pixel-by-pixel. Name is clyaksa. You can play with the libarary: clyaksa on clojars.

Now let me give you a quick snapshot of what this library is capable.

First of all, you should create or load:

(new-image 1000 :rgb)                    ;; 1000x1000 rgb image
(new-image 600 800 :argb)                ;; 600x800 argb image

(load-image :file "image.png")           ;; load image from filesystem
(load-image :url "google.com/image.png") ;; load image by url

Enabled image types:

  • :custom
  • :rgb
  • :argb
  • :argb-pre
  • :bgr

clyaksa use java.awt.image.BufferedImage. It means you don’t have to invoke new-image or load-image, you can use regular BufferedImage in all functions.

Now let’s draw something. Library clyaksa provides you the following methods:

Set pixel:

(set-pixel image 200 300 color)          ;; return changed image

Get pixel:

(get-pixel image 55 55)                  ;; return color of pixel

Swap pixels in one image:

(swap-pixel image x1 y1 x2 y2)           ;; return changed image

Swap pixels between two image:

(swap-pixel image1 image2 x1 y1 x2 y2)   ;; return array of changed images: [image1 image2]

Don’t forget color is int value.

If you need to get some information about picture you can use:

(get-height image)                       ;; get height of the image
(get-width image)                        ;; get width of the image

Finally, saving image function:

(save-image image "image.jpg")
(save-image image "image.jpg" :jpg)

Yes, clyaksa is a simple library. But every opportunity to write on Clojure pleases me. I hope this library will be useful to someone. On Github

comments powered by Disqus