Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns clojuredojo.core
- (:require [mikera.image.core :as img])
- (:import [java.awt.image BufferedImage]))
- (def pic-path "/Users/nikola/Downloads/lambda.png")
- (defn load-pic!
- "Reads a picture from a file."
- [path]
- (img/load-image path))
- (defn pic-size
- [^BufferedImage pic]
- [(.getHeight pic) (.getWidth pic)])
- (defn cell [pixels]
- (let [height (count pixels)
- width (count (first pixels))
- total (reduce + (flatten pixels))]
- (if (> total (/ (* height width) 2))
- "#"
- " ")))
- (defn cell-pixels [columns x y cell-h]
- (take cell-h
- (drop (* y cell-h)
- (first (drop x columns)))))
- (defn pic->columns
- [^BufferedImage pic grid-height grid-width]
- (let [[height width] (pic-size pic)
- cell-height (/ height grid-height)
- cell-width (/ width grid-width)
- pixels (seq (img/get-pixels pic))
- rows (partition width pixels)
- cell-rows (map (fn [r] (partition cell-width r)) rows)
- columns (apply mapv vector cell-rows)]
- columns))
- (defn columns->grid
- [columns grid-height grid-width]
- (for [x (range grid-width)
- y (range grid-height)]
- (cell (cell-pixels columns x y (quot (count (first columns)) grid-height)))))
Add Comment
Please, Sign In to add comment