Fork me on GitHub
#beginners
<
2023-12-14
>
quan xing06:12:30

In Clojure, how can I save the data;image/png;base64; to an image file? Are there any existing libraries available for this purpose?

seancorfield06:12:02

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.

hiredman06:12:48

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

seancorfield06:12:36

Good point. I wasn't sure if the OP needed to perform any image manipulation...

quan xing07:12:29

(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 :)

Abhishek21:12:50

https://www.hackerrank.com/challenges/lambda-march-compute-the-perimeter-of-a-polygon/problem?isFullScreen=false

;; 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?

kennytilton22:12:54

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?

1
kennytilton22:12:06

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

kennytilton22:12:02

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.

seancorfield22:12:52

(partition 2 1 (range 5)) maybe?

kennytilton22:12:34

Ah, I was just going to check partition. Perfect!

Abhishek08:12:21

Thank you very much! @U0PUGPSFR @U04V70XH6

👍 1
kennytilton14:12:34

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!