Skip to content

Instantly share code, notes, and snippets.

@wedesoft
Created October 28, 2020 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wedesoft/89f3310b257521361f0ace1e0b0d22dd to your computer and use it in GitHub Desktop.
Save wedesoft/89f3310b257521361f0ace1e0b0d22dd to your computer and use it in GitHub Desktop.
Save and load images using JMagick and Clojure
(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