Skip to content

Instantly share code, notes, and snippets.

@rotaliator
Last active January 29, 2019 08:13
Show Gist options
  • Save rotaliator/4a52b6764e86fd47bd8b3cde58138a83 to your computer and use it in GitHub Desktop.
Save rotaliator/4a52b6764e86fd47bd8b3cde58138a83 to your computer and use it in GitHub Desktop.
Save sequence of pixel values as grayscale image
(ns gist.core
(:require [clojure.string :as str])
(:import (java.io File)
(java.awt.image BufferedImage
WritableRaster)
(javax.imageio ImageIO))
(:gen-class))
(defn save-pixels-as-png
"Saves sequence of pixel values as grayscale image"
[pixels filename width height]
(let [bufferedImage (BufferedImage. width height BufferedImage/TYPE_BYTE_GRAY)
pixels (int-array pixels)
raster (.getRaster bufferedImage)
extension (last (str/split filename #"\."))]
(.setPixels raster 0 0 width height pixels)
(ImageIO/write bufferedImage extension (File. filename))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment