Skip to content

Instantly share code, notes, and snippets.

Created July 22, 2012 21:54
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save anonymous/3161135 to your computer and use it in GitHub Desktop.
(ns hook.image
(:use clojure.java.io)
(:import org.apache.sanselan.Sanselan
org.apache.sanselan.ImageFormat
java.awt.geom.AffineTransform
java.awt.image.AffineTransformOp))
(defn subimage
"Returns a subimage of the provided image."
([img {x :x y :y w :width h :height}] (subimage img x y w h))
([img x y w] (subimage img x y w w))
([img x y w h] (.getSubimage img x y w h)))
(defn translate
"Returns a translated version of the provided image."
[img x y]
(let [t (AffineTransform.)]
(.translate t x y)
(.filter (AffineTransformOp. t (AffineTransformOp/TYPE_BILINEAR)) img nil)))
(defn rotate
"Returns a rotated version of the provided image."
([img a] (rotate img a 0 0))
([img a x y]
(let [t (AffineTransform.)]
(.rotate t a x y)
(.filter (AffineTransformOp. t (AffineTransformOp/TYPE_BILINEAR)) img nil))))
(defn load [pth]
(Sanselan/getBufferedImage (file pth)))
(defn store
([img pth] (store img pth :tiff))
([img pth typ]
(let [typ-enum (case typ
:tiff ImageFormat/IMAGE_FORMAT_TIFF
:png ImageFormat/IMAGE_FORMAT_PNG
:jbig2 ImageFormat/IMAGE_FORMAT_JBIG2)]
(Sanselan/writeImage img (file pth) typ-enum nil))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment