view clojure_magick/magick.clj @ 9:23db8b1f0ee7

Softened tone in science minus science.
author Dylan Holmes <ocsenave@gmail.com>
date Sat, 29 Oct 2011 21:18:54 -0500
parents b4de894a1e2e
children
line wrap: on
line source
1 (ns clojure-magick.magick)
2 (import [org.im4java.core ConvertCmd IMOperation Stream2BufferedImage] )
4 (defn read-image "Read the image in the specified file; returns a BufferedImage." [filename]
5 (let [op (doto (IMOperation.)
6 (.addImage (into-array String [filename]))
7 (.addImage (into-array String ["-"])))
8 s2b (Stream2BufferedImage.)]
9 (doto (ConvertCmd.)
10 (.setOutputConsumer s2b)
11 (.run op (into-array Object [])))
12 (.getImage s2b))
13 )
15 (defn write-image "Write the image to the specified file." [img filename]
16 (let [op (doto (IMOperation.)
17 (.addImage)
18 (.addImage))]
19 (.run (ConvertCmd.) op (into-array Object [img filename])))
20 )
23 (defn glue-vertical "Concatenate images in a sequence from top to bottom, producing a new image." [image & imgs]
24 (let [imgs (cons image imgs)
25 op (doto (IMOperation.)
26 (.addImage (count imgs))
27 (.append)
28 (.addImage (into-array String ["-"])))
29 s2b (Stream2BufferedImage.)]
30 (doto (ConvertCmd.)
31 (.setOutputConsumer s2b)
32 (.run op (into-array Object (vec imgs))))
33 (.getImage s2b)
34 ))
36 (defn glue-horizontal "Concatenate images in a sequence from left to right, producing a new image." [image & imgs]
37 (let [imgs (cons image imgs)
38 op (doto (IMOperation.)
39 (.addImage (count imgs))
40 (.p_append)
41 (.addImage (into-array String ["-"])))
42 s2b (Stream2BufferedImage.)]
43 (doto (ConvertCmd.)
44 (.setOutputConsumer s2b)
45 (.run op (into-array Object (vec imgs))))
46 (.getImage s2b)
47 ))
51 (defn resize "Resize the image, keeping the same aspect ratio."
52 ([img width height]
53 (let [op (doto (IMOperation.)
54 (.addImage)
55 (.resize width height)
56 (.addImage (into-array String ["-"])))
57 s2b (Stream2BufferedImage.)]
58 (doto (ConvertCmd.)
59 (.setOutputConsumer s2b)
60 (.run op (into-array Object [img])))
61 (.getImage s2b)
62 ))
63 (
64 [img width]
65 (resize img width nil)
66 )
67 )
70 (defn upside-down "Flip the image upside down."
71 [img]
72 (let [op (doto (IMOperation.)
73 (.addImage)
74 (.flip)
75 (.addImage (into-array String ["-"])))
76 s2b (Stream2BufferedImage.)]
77 (doto (ConvertCmd.)
78 (.setOutputConsumer s2b)
79 (.run op (into-array Object [img])))
80 (.getImage s2b)
81 )
82 )
85 (defn scale-rotate-translate "Scale the image, then rotate the image about the axis coordinates, then translate the image by the given amount. Does not change the dimensions of the picture."
86 ([img axis-x axis-y scale-x scale-y degrees translate-x translate-y]
87 (let [arg-str (apply str (list axis-x "," axis-y " " scale-x "," scale-y " " degrees " " translate-x "," translate-y))
88 op (doto (IMOperation.)
89 (.addImage)
90 (.distort "ScaleRotateTranslate" arg-str)
91 (.addImage (into-array String ["-"])))
92 s2b (Stream2BufferedImage.)]
93 (doto (ConvertCmd.)
94 (.setOutputConsumer s2b)
95 (.run op (into-array Object [img])))
96 (.getImage s2b)))
97 ([img axis-x axis-y scale degrees translate-x translate-y]
98 (recur axis-x axis-y scale scale degrees translate-x translate-y))
99 )
102 (defn rotate "Rotate the image clockwise by the given amount." [img degrees]
103 (let [op (doto (IMOperation.)
104 (.addImage)
105 (.distort "ScaleRotateTranslate" (str degrees))
106 (.addImage (into-array String ["-"])))
107 s2b (Stream2BufferedImage.)]
108 (doto (ConvertCmd.)
109 (.setOutputConsumer s2b)
110 (.run op (into-array Object [img])))
111 (.getImage s2b))
112 )
139 (defn resize[]
140 (let [op (doto (IMOperation.)
141 (.addImage (into-array String ["/home/r/jiggly.gif"]))
142 (.resize 800 600)
143 (.addImage (into-array String ["/home/r/jiggly_sm.gif"]))
144 )]
145 (.run (ConvertCmd.) op (into-array Object []))))
148 (defn run-test[]
149 (let [puff (read-image "/home/r/jiggly.gif")]
150 (write-image (glue-horizontal puff puff puff) "/home/r/multijiggly.gif")
151 ))