Anonymous user
Grayscale image: Difference between revisions
→{{header|Clojure|Clojure}}
(→{{header|REXX}}: added a section to show how a "color" may be expressed in other forms in REXX.) |
|||
Line 213:
=={{header|Clojure|Clojure}}==
<lang clojure>
(ns rgb-to-gray.clj▼
'[javax.imageio ImageIO]
▲ (:import [java.io File]
(let [width (.getWidth color-image)]
(partition width
r (.getRed rgb-object) ▼
▲ (
▲ fn [x]
▲ let [rgb (.getRGB color-image x y) rgb-object (new Color rgb) ]
▲ ;Compute the grayscale value an return it: L = 0.2126·R + 0.7152·G + 0.0722·B
▲ + (* r 0.2126) (* g 0.7152) (* b 0.0722)
▲ )
▲ (
▲ .getHeight color-image
▲ )
(defn write-matrix-to-image [matrix filename]
(ImageIO/write
(ImageIO/write (let [height (count matrix) width (count (first matrix)) output-image (new BufferedImage width height BufferedImage/TYPE_BYTE_GRAY)]▼
(let [height (count matrix)
width (count (first matrix))
▲
column-index (range width)]
"png"
▲ .setRGB output-image column-index row-index (.intValue (nth (nth matrix row-index) column-index))
▲ (range width)
▲ (range height)
▲ output-image
▲ "png" (new File filename))
(println
(write-matrix-to-image
(rgb-to-gray
(ImageIO/read (new File "test.jpg")))
"test-gray-cloj.png"))▼
▲ "test-gray-cloj.png")
</lang>
|