Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Forked from skuro/README.md
Last active May 16, 2018 19:32
Show Gist options
  • Save jeroenvandijk/52befaf39a078c57e7df9aac2decc600 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/52befaf39a078c57e7df9aac2decc600 to your computer and use it in GitHub Desktop.
Dutch Clojure Meetup #102
(ns clojure-dojo.core)
(import '[javax.imageio ImageIO]
'[java.awt.image BufferedImage])
(def img (let [f "resources/image.png"]
(ImageIO/read (clojure.java.io/file f))))
(def img (let [f "resources/image.png"]
(ImageIO/read (clojure.java.io/file f))))
(class img)
(defn resize-image [img width height]
(let [tmp (.getScaledInstance img width height java.awt.Image/SCALE_SMOOTH)
dimg (BufferedImage. width height BufferedImage/TYPE_INT_ARGB)
g2d (.createGraphics dimg)]
(.drawImage g2d tmp 0 0 nil)
(.dispose g2d)
dimg))
(def scaled-img (resize-image img 20 20)
)
(let [f "resources/image.png"]
(count (.getBytes (clojure.java.io/file f))))
(defn black? [x]
(zero? x))
(defn mmap [f rows]
(mapv
(fn [row]
(mapv (fn [col]
(f col))
row))
rows))
;(def width 100)
;(def height 167)
(.getHeight img)
(defn img->matrix [img]
(->>
(mapv (fn [y]
(mapv (fn [x]
(.getRGB img x y))
(range (.getWidth img))))
(range (.getHeight img)))))
(def matrix (img->matrix img))
(class scaled-img)
(def scaled-matrix (img->matrix scaled-img))
(bean scaled-matrix)
(defn bool->int [b]
(if b 0 1))
(defn matrix->string [matrix]
(->> matrix
(map (fn [row] (clojure.string/join "" row)))
(clojure.string/join "\n" )))
(spit "matrix.edn" (matrix->string (mmap (comp bool->int black?) matrix)))
(spit "scaled.img" (matrix->string (mmap (comp bool->int black?) scaled-matrix)))
(defn count-non-zeros [col]
(count (remove zero? col)))
(defn transpose-matrix [matrix]
(let [height (count matrix)
width (count (first matrix))]
(mapv (fn [y]
(mapv (fn [x]
(nth (nth matrix x) y))
(range width)))
(range height))))
(defn transpose-matrix [matrix]
(let [height (count matrix)
width (count (first matrix))]
(mapv (fn [x]
(mapv (fn [y]
(nth (nth matrix y) x))
(range height)))
(range width))))
(transpose-matrix [[1 1]
[0 1]])
(transpose-matrix [[1 1]
[0 1]
[0 1]])
(transpose-matrix [[1 1 0]
[0 1 1]])
(defn prepad [n seq]
(let [extra (- n (count seq))]
(concat (repeat extra "' ") seq)))
(prepad 10 [1 2 3 4 5])
(defn coll-stats [coll]
(->>
coll
(partition-by zero? )
(remove (fn [xs]
(zero? (first xs))) )
(map count)))
(defn matrix->csv [sep matrix]
(clojure.string/join "\n" (map (fn []) matrix))
)
(let [header
(->> scaled-matrix
(transpose-matrix )
(mapv coll-stats)
(map (fn [stats]
(prepad 5 stats)))
(transpose-matrix)
(map #(prepad 5 %)))
rows (map (fn [stats]
(prepad 5 stats))
(mapv coll-stats scaled-matrix)
)
]
(->>
(concat header rows)
(map #(clojure.string/join " ;" %))
(clojure.string/join "\n" )
(spit "puzzle.csv")))
(mmap (comp bool->int black?) scaled-matrix)
*
(let [row-stats (mapv coll-stats scaled-matrix)
col-stats (mapv coll-stats (transpose-matrix scaled-matrix))]
(spit "stats.img" (matrix->string (vec (map-indexed (fn [i row]
row ;(conj row (nth row-stats i))
)
scaled-matrix))))
)
(map )
#_(spit "matrix.edn" (matrix->string (mmap (comp bool->int black?) matrix)) #_ (with-out-str (prn #_clojure.pprint/pprint (mmap (comp bool->int black?) matrix))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment