trying to get data out using versions of -output. I tried .txt and .asc Problem is to get custom outputs.
So I have
gmic script.gmic img.png -foo
foo :
--colormap. 8 .round. index.. [1] -rm.
-s y -compress_rle , -rows 6,100%
# compress_rle generates a column vector, so roll into a row vector, and add all row vectors together
-y x -a y
-o. picture.txt
So I am looking for a line by line rle type output, with each output line matching a pixel row.
However, the output is padded on the right by zeros.
How would I remove the 0’s ?
I wanted to output row by row, appending to a single file. But couldn’t find a neat way to do this.
Also I found the function -img2string which produced a string in a form I could use, but I couldn’t work out how to put the string into an output file.
The aim is to take data generated by gmic for use by other programs.
Whilst I can easily see how the formats .asc, .txt and .h are structured, I couldn’t quickly find the format for .cimg
Looks a bit tricky, but this is actually easy to do
I propose this :
foo :
--colormap. 8 -round. -index.. [1] -rm.
-s y -compress_rle , -rows 6,100%
-repeat $! -l[$>] ({'{^}\n'}) -k. -endl -done
-a x -o raw:picture.txt,uchar
The idea is to replace each vector image resulting of -compress_rle by a vector containing its ASCII representation + a newline at the end. For this, I use the substituting expression {^} which is replaced by a string describing the values of the last image, separated by commas. I put it in an input expression like ({'expr'})' to construct an image from the ASCII codes of expr. Once I get all the ASCII conversions for each line, I append the lines and save the file as a raw file of unsigned char` values to get the resulting file.
I found the ^ character under the substitution part of the documentation. But it seemed to me that I should be using {image,^} and I could not get any combination of { [0], ^ } to work. I know that it says the “the image can be eluded”, but I understood that to mean ‘omitted’ which I thought would mean { ,^}. Some short examples in this section might help.
Regarding the -k. : Is this to replace the local image with the ascii vector?
There is a slight problem:
The first element in the string is prefixed with some number (random?) of ‘\0’ chars.
Eg. expecting: "-2", "2", "4", "-4", "6", "1", "-8", "0", "2", "-3", "6"
got "\0\0\0\0\0\0-2", "2", "4", "-4", "6", "1", "-8", "0", "2", "-3", "6"
The correct syntax to refer to image [0] is {0,^} , no need for extra braces.
And when the image indice is eluded, the comma is not necessary, so {^} instead of {,^}
(just as {w} is an equivalent expression to {-1,w}).
In your case, it will be equivalent, as the ASCII codes generated are in range [0,127] in any case. For saving an ASCII file, char and uchar can be used the same actually.
That’s unexpected yes. Any simple example to reproduce this ?
It means maybe that one of your vector had less than 6 values, and the -rows command produce then a vector of 6 zeros.
I can send you the gmic file and a small (320x312) image that produces this result. How should I send? pastebin?
Here is the gmic file (small)
make_col_json :
-rr2d 20,20
--colormap. 8 -round.
out="{\n\t\"colors\": {\n\t\t"
sep=",\n\t\t"
-repeat {w}
-if {!$<} sep="\n\t}\n}\n" -endif
out=$out"\"C"{$>}"\": [ "{I[$>]}" ]"$sep
-done
({'$out'}) -o. raw:colour.json,uchar
-rm.
make_paint_file :
# assumes that there are two files on stack: image and colour map
-index.. [1]
-rm.
# remove the colour map
-s y
-compress_rle 0,0
-rows 6,100%
-repeat $! -l[$>] ({'{^}\n'}) -k. -endl -done
-a y
-o. raw:picture.txt,uchar
run in terminal (Ubuntu) with
gmic -i img.png -make_col_json -make_paint_file
I don’t think the image file is important. I generated it from GIMP cutting out a small part of a standard picture.
Note that the initial \0 chars are not visible in a normal editor. But when the data is processed by a program that does not assume char data, the \0 show up.
Also @richardh, I’ve re-implemented the command -colormap yesterday, and it should be really faster to compute (especially when the number of desired colors is high).
I’d be interested by your feedback on this, just try $ gmic -update and it should use the new version.