Last active
September 14, 2016 19:28
-
-
Save skuro/9483015cad5666f3c9f090815b416f53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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