Last active
January 29, 2019 08:13
-
-
Save rotaliator/4a52b6764e86fd47bd8b3cde58138a83 to your computer and use it in GitHub Desktop.
Save sequence of pixel values as grayscale image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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