Skip to content

Instantly share code, notes, and snippets.

@skuro
Last active September 14, 2016 19:28
Show Gist options
  • Save skuro/9483015cad5666f3c9f090815b416f53 to your computer and use it in GitHub Desktop.
Save skuro/9483015cad5666f3c9f090815b416f53 to your computer and use it in GitHub Desktop.
(ns nono.core
(:import [javax.imageio ImageIO]
[java.io File]))
(def test-path "/Users/not-going-to-show-my-user/Downloads/sample.bmp")
(defn extract-rgb [pixel]
(let [r (bit-and 0x000000FF (bit-shift-right pixel 16))
g (bit-and 0x000000FF (bit-shift-right pixel 8))
b (bit-and 0x000000FF pixel)]
(/ (+ r g b) 3)))
(defn read-image [path]
(let [file (File. path)
img (ImageIO/read file)
h (.getHeight img)
w (.getWidth img)]
(mapv (fn [row-idx]
(mapv (fn [col-idx]
(-> img
(.getRGB col-idx row-idx)
extract-rgb))
(range 1 w)))
(range 1 h))))
(defn pixelate [pixels size]
(let [cols (quot (count (first pixels)) size)
rows (quot (count pixels) size)]
(->> pixels
(mapv (fn [row]
(->> row
(partition-all cols cols)
(mapv #(apply + %)))))
(partition-all rows rows)
(mapv #(->> % (apply mapv +) make-px)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment