Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by

I need to use this library https://github.com/silvia-odwyer/photon and I use this example https://github.com/minimal-xyz/minimal-shadow-cljs-nodejs as a starting point and I want to replicate this piece of code to test it:

   var fs = require('fs');

var photon = require("@silvia-odwyer/photon-node");

const fetch = require('node-fetch');
global.fetch = fetch;

function grayscaleImage() {
    // read file, then convert to base64
    var base64 = fs.readFileSync(`input.png`, { encoding: 'base64' });
    let data = base64.replace(/^data:image\/(png|jpg);base64,/, "");

    // convert base64 to PhotonImage
    var phtn_img = photon.PhotonImage.new_from_base64(data);

    photon.grayscale(phtn_img);

    // get base64 from filtered image, and write 
    let output_base64 = phtn_img.get_base64();
    let output_image_name = "output.png";
    var output_data = output_base64.replace(/^data:image\/\w+;base64,/, '');

    fs.writeFile(output_image_name, output_data, {encoding: 'base64'}, function(err) {
    });
    console.log(`Saved ${output_image_name}`);

}

grayscaleImage();

So I wrote this

(ns server.main
  (:require ["@silvia-odwyer/photon-node" :as sop]
                    ["fs" :as fs]
                    ["node-fetch" :as nf]
                    [clojure.string :as string]))

(defn main! []
  (let [base64 (fs/readFileSync "input.png" #js {:encoding "base64"})
        data (.replace base64 #"^data:image/(png|jpg);base64," "")
        phtn-img (sop/PhotonImage.new_from_base64 data)
        phtn-img (sop/grayscale phtn-img)
        output-base64 (sop/get_base64 phtn-img)]
    (println output-base64)))

but when I run
'shadow-cljs release app' and ' node target/main.js'
It throws:

    C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs\target\mai
n.js:230
Vc.prototype.Z=B;Vc.prototype.J=function(a,b){return ob(b,"()")};ra.pr
ototype.Z=B;ra.prototype.J=function(a,b,c){return Te(this,Se,b,c)};Ie.
prototype.Z=B;Ie.prototype.J=function(a,b,c){return Ke(b,Se,"("," ",")
",c,this)};tc.prototype.Z=B;tc.prototype.J=function(a,b,c){return Ke(b
,Se,"("," ",")",c,this)};"undefined"!==typeof console&&za();za();var y
a=new Qb(null,"print-length","print-length",1931866356),sa=new Qb(null
,"flush-on-newline","flush-on-newline",-151457939),ua=new Qb(null,"met
a","meta",1499536964),Ve=new Qb(null,"fallback-impl","fallback-impl",-
1501286995),ta=new Qb(null,"readably","readably",1129599760),Ue=new Qb
(null,"alt-impl","alt-impl",670969595),va=new Qb(null,"dup","dup",5562
98533),Le=new Qb(null,"more-marker","more-marker",-14717935);var af=re
quire("@silvia-odwyer/photon-node");var cf=require("fs");require("node
-fetch");pd(function(){$e(qc(["App loaded!"]));$e(qc(["asdasd"]));var
a=cf.readFileSync("input.png",{encoding:"base64"}).replace(/^data:imag
e\/(png|jpg);base64,/,"");a=af.PhotonImage.new_from_base64(a);a=af.gra
yscale(a);a=af.get_base64(a).replace(/^data:image\/\w+;base64,/,"");re
turn $e(qc([a]))},process.argv.slice(2));

TypeError: af.get_base64 is not a function
    at Function.<anonymous> (C:\Users\usuario\desktop\clojure\minimal-
shadow-cljs-nodejs\target\main.js:230:1066)
    at pd (C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs
\target\main.js:112:412)
    at C:\Users\usuario\desktop\clojure\minimal-shadow-cljs-nodejs\tar
get\main.js:230:850
    at Object.<anonymous> (C:\Users\usuario\desktop\clojure\minimal-sh
adow-cljs-nodejs\target\main.js:231:3)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1
035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/r
un_main.js:60:12)
    at internal/main/run_main_module.js:17:47

Saying that get_base64 is not a function...
doc: https://docs.rs/photon-rs/0.2.0/photon_rs/struct.PhotonImage.html#method.get_base64

I don't know if node-fetch can be related to this

I'm doing something wrong?

1 Answer

0 votes
by

(sop/get_base64 phtn-img) should be (.get_base64 phtn-img)

You can use this app to see how to translate JS calls into cljs

https://roman01la.github.io/javascript-to-clojurescript/

by
Ok, but why? isn't a photon function? or is it javascript?
On the other hand when I apply grayscale, the function returns nil instead of the object, do you know how to get the object itself? I tried with Object.values but it doesn't work.
Welcome to Clojure Q&A, where you can ask questions and receive answers from members of the Clojure community.
...