Skip to content

Instantly share code, notes, and snippets.

@philopon
Created March 10, 2013 17:39
Show Gist options
  • Save philopon/5129592 to your computer and use it in GitHub Desktop.
Save philopon/5129592 to your computer and use it in GitHub Desktop.
(ns excel-oekaki.core
(:import
(java.io File FileOutputStream FileInputStream)
(java.awt Color)
(javax.imageio ImageIO)
(org.apache.poi.xssf.usermodel XSSFWorkbook XSSFColor)
(org.apache.poi.ss.usermodel CellStyle)
))
(defn -main [iname oname]
(with-open [out (FileOutputStream. oname)]
(let [image (ImageIO/read (File. iname))
wb (XSSFWorkbook.)
sheet (.createSheet wb "oekaki")
dict (atom (array-map))]
(dorun
(for [y (range (.getMinY image) (.getHeight image))]
(let [row (.createRow sheet y)]
(println y (count @dict))
(dorun
(for [x (range (.getMinX image) (.getWidth image))]
(let [cell (.createCell row x)
rgb (.getRGB image x y)
inDict (find @dict rgb)]
(if inDict
(.setCellStyle cell (last inDict))
(let [style (.createCellStyle wb)
color (XSSFColor. (Color. (.getRGB image x y)))]
(.setFillForegroundColor style color)
(.setFillPattern style CellStyle/SOLID_FOREGROUND)
(.setCellStyle cell style)
(swap! dict assoc rgb style)))))))))
(.write wb out))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment