output data eg RBG as json

(Richard Hainsworth) #1

I can’t work out how to form data files and output them. I found a sequence in the Intarsia function that seems to create text using the pattern ( { ’ " Some text " ’ } ) # spaces for clarity
({’“Some text”’})
and then I would think later, something like
-o raw:“data.txt”,uchar

However, the interpreter stops at (
My understanding is that I am trying to add text into an ‘image’ and then outputting the ‘image’ as uchar
But I can’t see yet how to create the text images

What I am trying to do is to decompose an image into RGB colors using -colormap 8 and then save the values of the colors into (eg) a json file, looking for the output
{ “colors”: {“C1”: [ 12,34,56 ], “C2”: [54,124,22], (…) }}
I have:
gmic -i img.pn --colormap 8 -round[1] 0 ({’"{“colors”: {\n"’}) -a[-2,-1] x

But I get
Syntax error: “(” unexpected
in the first line.

Not sure where I am going wrong

(Richard Hainsworth) #2

After a lot of experimenting, it appears to be an escaping issue. So in a terminal (Ubuntu)

gmic ({'"hello world"'}) 

fails and generates an error

gmic \(\{\'\"hello world\"\'\}\)

adds the text to the image.

(David Tschumperlé) #3

Hello Richard,

When you have to manipulate such ‘complex’ expressions (with { or ( ), I would suggest you create your own custom command in a separate .gmic file, and invoke it from the command line, instead of trying to do all these stuffs on the command line directly. Mainly because bash has its own substitution mechanism, and the braces and parentheses have also a meaning for him. So if you want to pass those character to the G’MIC interpreter, this requires a lot of attention and backslashs ! :stuck_out_tongue:

What I would do here, is to create a file foo.gmic containing :

foo :
  -colormap. 8 -round.
  out="\"colors\": {"
  sep=", "
  -repeat {w}
    -if {!$<} sep=" }}" -endif
    out=$out"\"C"{1+$>}"\": [ "{I[$>]}" ]"$sep
  ({'$out'}) -o. raw:output.json,uchar

So I know that I can write G’MIC stuffs exactly how it will be read by the G’MIC interpreter, and won’t have to care about possible bash substituting expressions.

Then I can invoke it from bash like this :

$ gmic goo.gmic lena.bmp -foo

In the function above, I just build a string in a variable $out, then save it as a raw ascii file. I think the code is easy enough to be understood without any further explanations :slight_smile: But of course, if you need them, please tell me !

(Richard Hainsworth) #4

Thanks for this help.
I was just coming around to the same view.
And wondering quite how to do it.
So this snippet helps a lot.

The sep variable is a cool way of ending the list. I had put off thinking about that until I got the text working.
So thanks again for this trick.