This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-12-14
Channels
- # adventofcode (29)
- # aws (3)
- # babashka (25)
- # beginners (13)
- # calva (4)
- # cherry (7)
- # cider (26)
- # clj-kondo (9)
- # clojure (88)
- # clojure-europe (21)
- # clojure-losangeles (3)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-uk (11)
- # clojuredesign-podcast (2)
- # clojurescript (4)
- # cursive (10)
- # datalevin (1)
- # emacs (50)
- # gratitude (1)
- # honeysql (12)
- # hyperfiddle (19)
- # jobs-discuss (28)
- # kaocha (3)
- # lsp (53)
- # malli (4)
- # meander (3)
- # off-topic (48)
- # re-frame (11)
- # releases (2)
- # ring-swagger (2)
- # shadow-cljs (50)
- # squint (26)
- # tools-build (3)
- # tools-deps (8)
- # xtdb (4)
- # yamlscript (1)
In Clojure, how can I save the data;image/png;base64; to an image file? Are there any existing libraries available for this purpose?
We use https://github.com/mikera/imagez at work for nearly all our image processing. You can decode from base 64 to binary using Java interop.
You don't need to use any libraries, java has a built in base64 codec that will turn that to a byte array, then just write it to disk. I believe the base64 codec may even work on streams so you can just write it directly to disk and it will get decoded from base64 as it goes
Good point. I wasn't sure if the OP needed to perform any image manipulation...
(let [
base64-str "data:image/jpeg;base64,/9j/4AAQSkZJ..."
base64 (second (str/split base64-str #","))
base64-byte (.decode (Base64/getDecoder) base64)]
(io/copy base64-byte (io/file "test.png")))
It's working. Thanks :);; funtion to calculate distance between two cordinates p1,p2
(defn dis [p1 p2]
(Math/sqrt ( + (Math/pow (- (first p1) (first p2)) 2) (Math/pow (- (second p2) (second p1)) 2))))
;;code for input/output
(let [no_of_pairs (Integer/parseInt (read-line)) pairs (loop [m no_of_pairs lst '()]
(if (= m 0)
lst
(recur (- m 1 )( conj lst (map #(Integer/parseInt %) (clojure.string/split (read-line) #"\s+"))))))]
(print pairs)) ;;=> ((681 463) (551 990) (1043 770))
the problem is to calculate perimeter of polygon.
how am supposed to pass the coordinates in the pairs
sequence to defn dis
from the lower code?You can loop
on the list of coordinates, destructuring two at a time. Or I guess just destructure in a summing function, returning zero when given only one point. Need some code?
I got curious, because I am a Common Lisper, where we could just (loop on [a b] pts summing ...)
. "on" is a terse way of saying progress iteratively from a list to the rest
of the list. We also could do sth very close to this trick, bypassing destructuring:
(let [pts (range 5)]
(map (fn [a b & _]
(prn a b))
pts (rest pts)))
0 1
1 2
2 3
3 4
Much slicker (from SO):
(let [v (range 5)]
(for [pair (map vector v (rest v))]
pair) )
=> ([0 1] [1 2] [2 3] [3 4])
I suspect native Clojurians will be able to improve on that.(partition 2 1 (range 5))
maybe?
Ah, I was just going to check partition
. Perfect!
Doh! :face_palm: You already had the pairs, @U06AHBND5EU! So now just roll your code up into a form that returns the pairs:
(let [no_of_pairs ...
pairs ...]
pairs) ; <=== instead of printing, return the pairs
...and wrap that ^^ in a caller that can pass each pair to dis
to get a list of distances, then sum those.
Sorry for misreading your Q!