Created
October 28, 2020 21:14
-
-
Save wedesoft/89f3310b257521361f0ace1e0b0d22dd to your computer and use it in GitHub Desktop.
Save and load images using JMagick and Clojure
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
(import '[magick MagickImage ImageInfo ColorspaceType]) | |
(defn spit-image [file-name width height data] | |
"Save an RGB image" | |
(let [info (ImageInfo.) | |
image (MagickImage.)] | |
(.constituteImage image width height "RGB" data) | |
(.setSize info (str width \x height)) | |
(.setDepth info 8) | |
(.setColorspace info ColorspaceType/RGBColorspace) | |
(.setFileName image file-name) | |
(.writeImage image info) | |
image)) | |
(defn slurp-image [file-name] | |
"Load an RGB image" | |
(let [info (ImageInfo. file-name) | |
image (MagickImage. info) | |
dimension (.getDimension image)] | |
(.setDepth info 8) | |
(.setColorspace info ColorspaceType/RGBColorspace) | |
(.setMagick info "RGB") | |
[(.width dimension) (.height dimension) (.imageToBlob image info)])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment