Guest User

Untitled

a guest
May 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. (ns clojuredojo.core
  2. (:require [mikera.image.core :as img])
  3. (:import [java.awt.image BufferedImage]))
  4.  
  5.  
  6. (def pic-path "/Users/nikola/Downloads/lambda.png")
  7.  
  8. (defn load-pic!
  9. "Reads a picture from a file."
  10. [path]
  11. (img/load-image path))
  12.  
  13. (defn pic-size
  14. [^BufferedImage pic]
  15. [(.getHeight pic) (.getWidth pic)])
  16.  
  17. (defn cell [pixels]
  18. (let [height (count pixels)
  19. width (count (first pixels))
  20. total (reduce + (flatten pixels))]
  21. (if (> total (/ (* height width) 2))
  22. "#"
  23. " ")))
  24.  
  25. (defn cell-pixels [columns x y cell-h]
  26. (take cell-h
  27. (drop (* y cell-h)
  28. (first (drop x columns)))))
  29.  
  30. (defn pic->columns
  31. [^BufferedImage pic grid-height grid-width]
  32. (let [[height width] (pic-size pic)
  33. cell-height (/ height grid-height)
  34. cell-width (/ width grid-width)
  35. pixels (seq (img/get-pixels pic))
  36. rows (partition width pixels)
  37. cell-rows (map (fn [r] (partition cell-width r)) rows)
  38. columns (apply mapv vector cell-rows)]
  39. columns))
  40.  
  41. (defn columns->grid
  42. [columns grid-height grid-width]
  43. (for [x (range grid-width)
  44. y (range grid-height)]
  45. (cell (cell-pixels columns x y (quot (count (first columns)) grid-height)))))
Add Comment
Please, Sign In to add comment