How can I define width/height of svg-sprite and write these values to the file (with Linux or Mac CLI) - imagemagick

I'm trying to create svg-sprite using npm package svg-sprite. Eventually I get sprite, that looks like this:
// icons-test.svg
...
<svg viewBox="0 0 108 54" ...>
<svg height="54" width="54" viewBox="-2 -2 54 54" ...>
<path d="...”>
</svg>
<svg height="54" width="54" viewBox="-2 -2 54 54" ...>
<path d="...”>
</svg>
</svg>
To define size (for example width) of this svg-sprite I use command identify from util ImageMagick.
identify -format '%w' icons-test.svg
or write it to the file
echo "\$spriteWidth = $(identify -format ‘%w’ icons-test.svg)px" >> styles.styl
The problem is that in file I don't get width of full svg-sprite (108), but only width of last sub-svg image(54), that is included in common svg-sprite.
Please, tell me where is my mistake? How to make identify return correct width.
Or advice me another variants to solve the problem.

I would suspect that the nested svg tags are confusing ImageMagick's internal SVG decoder. Try converting the SVG into MVG. It should throw-out the nested SVG structure.
$ convert icons-test.svg mvg:-
Which will print the following MVG instructions.
push graphic-context
viewbox 0 0 108 54
affine 1 0 0 1 0 0
push graphic-context
viewbox 0 0 54 54
affine 1 0 0 1 2 2
push graphic-context
path ''
pop graphic-context
pop graphic-context
push graphic-context
viewbox 0 0 54 54
affine 1 0 0 1 2 2
push graphic-context
path ''
pop graphic-context
pop graphic-context
pop graphic-context
With the nested viewboxs isolated to graphic-contexts on the stack, you should be able to identify correctly.
$ convert icons-test.svg mvg:- | identify -format '%w' mvg:-
#=> 108

Related

ImageMagick `-duplicate` but to beginning of sequence

-duplicate
convert img*.png -duplicate 3 out.gif
makes
0 1 2 2 2 2
where 0 is img0.png, and 1 is img1.png, etc, making a GIF. But can I make below?
0 0 0 0 1 2 2 2 2
that is, append to end and start? I get I can use indexing to make
0 1 2 2 2 2 0 0 0
which is identical in a loop, but I need the 0s at start in context.
TL;DR for 0 0 0 1 2 3 ... 20 21 21 21 (any number of images) do
convert img*.png -write mpr:imgs -delete 0--1 mpr:imgs[0,0,0,1--1,-1,-1] out.gif
Using ImageMagick v6 on Windows command line, this command will let you arrange the order and number of images in any way you need.
convert img*.png -write mpr:imgs -delete 0--1 mpr:imgs[0,1,2,2,2,2,0,0,0] out.gif
That reads the 3 input images, copies them all into a memory register named "mpr:imgs", deletes the input images from the command, then reads the images from that memory register as you specify in the square brackets. The image index 0 is the first image read into the command, 1 is the second, etc.
Also, using -duplicate you can specify which image in the list you want to use according to their order in the list. This command will give the same result as the one above...
convert img*.png -duplicate 3 -duplicate 3,0 out.gif
The command reads the three images, then -duplicate 3 makes 3 more of the last image in the list, then -duplicate 3,0 makes 3 more of the first image in the list, index 0.
Another approach would be to use ( -clone ... ) inside parentheses to create the number of duplicates in the order you want.
convert img*.png ( -clone 2,2,2 -clone 0,0,0 ) out.gif
That reads the 3 input images, clones the third image 3 times, then clones the first image 3 times, giving the same result as the commands above.
These commands are in Windows syntax. For a *nix OS you'd have to escape the parentheses with backslashes "\(...\)".
Also helpful are -swap, -reverse, -insert, and -delete to manipulate the order of images in the list.
You should be able to load your first image, make any duplicates you need, then load the second and make duplicates and load the third and make any duplicates:
convert img0.png -duplicate 3 img1.png img2.png -duplicate 3 ...
will give your desired image sequence:
0 0 0 0 1 2 2 2 2

Software/tool to generate R-G-B values of every pixel from an image and vice-versa

Is there a software/tool that can generate me a matrix of RGB values from a simple raw 8-bit RGB image?
Also, is there a software/tool that can generate an image from a given matrix of RGB values?
Thank you.
PS:
i) I am aware that this can be done using Matlab. I am looking for a tool that can do it that is not Matlab.
ii) I am aware of existing question about doing similar stuff programmatically. I need a software tool, if there is any, that can do this task.
I would suggest you use the venerable NetPBM which is available for Linux, macOS and Windows. Alternatively, you could use ImageMagick but that is much heavier weight, see later.
NetPBM Method - see Wikipedia NetPBM entry
So, let's start with a raw, 8-bit RGB file that contains a red, a green and a blue pixel:
-rw-r--r-- 1 mark staff 9 10 Oct 07:47 rgb888.bin
As you can see, it has 9 bytes. Let's look at them:
xxd -g3 rgb888.bin
00000000: ff0000 00ff00 0000ff
Now, if we want that image as a matrix of legible values:
rawtoppm -plain 3 1 rgb888.bin
Sample Output
P3
3 1
255
255 0 0 0 255 0 0 0 255
where:
-plain means to display in ASCII rather than binary
P3 tells us it is colour and ASCII
3 1 tells us its dimension are 3 pixels wide by 1 pixel high
255 essentially tells us it is 8-bit (65536 would mean 16-bit)
the last row is the pixels
Converting back to binary is a little harder, let's assume we start with a PPM file created like this:
rawtoppm -plain 3 1 rgb888.bin > image.ppm
So, we can get the binary version like this:
ppmtoppm < image.ppm | tail -c 9 > rgb888.bin
and look at it with:
xxd -g3 rgb888.bin
00000000: ff00 0000 ff00 0000 ff
ImageMagick Method
# Convert binary RGB888 to text
convert -depth 8 -size 3x1 RGB:rgb888.bin txt:
Sample Output
# ImageMagick pixel enumeration: 3,1,65535,srgb
0,0: (65535,0,0) #FF0000 red
1,0: (0,65535,0) #00FF00 lime
2,0: (0,0,65535) #0000FF blue
Or, slightly different appearance:
# Convert binary RGB888 to matrix
convert -depth 8 -size 3x1 RGB:rgb888.bin -compress none ppm:
Sample Output
P3
3 1
255
255 0 0 0 255 0 0 0 255
And now going the other way, PPM to binary
# Convert PPM image to binary
convert image.ppm rgb:image.bin
# Check how the binary looks
xxd -g 3 image.bin
00000000: ff0000 00ff00 0000ff .........
Plain dump method
Maybe you are happy with a plain dump from od:
od -An -t u1 rgb888.bin
Sample Output
255 0 0 0 255 0 0 0 255

Unabe to create samples using opencv_createsamples.exe

Im trying to train a haar cascade. For that as a test run, I'm taking 5 positive images (which have the image). I use a program called objectmarker.exe to mark the object in the image and store the coordinates as well as the height and width of the rectangle in a text file (positives.txt)
Now when I try to create a .vec file using the the text file from command line, the program executes, but i get the following:
positive(1).txt : parse errorDone. Created 0 samples
The .vec file does get generated but if i try to view it, it opens a window and crashes.
I use the following code
C:\Sahil\Major Project\Haartraining Stuff\Haartraining Stuff\STEPS\step 02>openc
v_createsamples.exe -info positives.txt -num5 -vec vec5.vec -w 20 -h 20
Info file name: positives.txt
Img file name: (NULL)
Vec file name: vec5.vec
BG file name: (NULL)
Num: 1000
BG color: 0
BG threshold: 80
Invert: FALSE
Max intensity deviation: 40
Max x angle: 1.1
Max y angle: 1.1
Max z angle: 0.5
Show samples: FALSE
Width: 20
Height: 20
Create training samples from images collection...
positives.txt(1) : parse errorDone. Created 0 samples
my postives.txt is in the following format
C:/Sahil/Major Project/Haartraining Stuff/Haartraining Stuff/STEPS/step 02/rawdata/00007 001 (3).bmp_0000_0065_0107_0107_0199.bmp 1 1 2 106 193
C:/Sahil/Major Project/Haartraining Stuff/Haartraining Stuff/STEPS/step 02/rawdata/00007 001 (4).bmp_0000_0065_0107_0107_0199.bmp 1 1 2 108 195
C:/Sahil/Major Project/Haartraining Stuff/Haartraining Stuff/STEPS/step 02/rawdata/00007 001.bmp_0000_0065_0107_0107_0199.bmp 1 2 5 110 195
C:/Sahil/Major Project/Haartraining Stuff/Haartraining Stuff/STEPS/step 02/rawdata/img1.bmp 1 4 4 103 190
C:/Sahil/Major Project/Haartraining Stuff/Haartraining Stuff/STEPS/step 02/rawdata/img2.bmp 1 3 5 118 217
kindly suggest what i can do to correct this error. as i cannot proceed further
How is opencv_createsamples.exe distinguishing image file names? It might be written not to check white characters in paths/file names. Try without spaces either in the paths and file names.

How can i read svg data stroke in pycairo?

I have JPG images and with inputsvgdraw, a flash tool for image annotation (http://www.mainada.net/inputdraw), i can trace lines on it that generate svg datas.
svg datas sample:
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 488 325"><g fill="none" stroke-miterlimit="6" stroke-linecap="round" stroke-linejoin="round"><path d="M 307 97 l 0 -1 l -2 -1 l -10 -2 l -20 -1 l -25 5 l -22 9 l -10 9 l 0 9 l 2 12 l 16 18 l 25 11 l 25 5 l 17 -1 l 6 -4 l 3 -7 l -1 -12 l -6 -16 l -7 -13 l -11 -12 l -11 -14 l -9 -5" opacity="1" stroke="rgb(170,37,34)" stroke-width="5"/></g></svg>.
What function can manage this data ?
You can read the SVG input by using librsvg and then rendering it with cairo. If you want to draw the annotations in SVG over the initial image, you might need to use PIL with numpy since cairo itself doesn't load many different image formats.
Following is an example that achieves that (the only difference is that actually I tested it with an ad-hoc ctypes wrapper for rsvg):
import sys
import rsvg
import cairo
import numpy
from PIL import Image
# Load an image that supposedly has the same width and height as the svg one.
img_rgba = numpy.array(Image.open(sys.argv[1]).convert('RGBA'))
data = numpy.array(img_rgba.tostring('raw', 'BGRA'))
width, height = img_rgba.size
surface = cairo.ImageSurface.create_for_data(data,
cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)
# "Paste" the svg into the image.
svg = rsvg.Handle(file=sys.argv[2])
svg.render_cairo(ctx)
surface.write_to_png(sys.argv[3])
cairosvg can load SVG files to a pycairo ImageSurface:
from cairocffi import ImageSurface
from cairosvg.parser import Tree
from cairosvg.surface import PNGSurface
def load_svg(svg: bytes) -> ImageSurface:
return PNGSurface(Tree(bytestring=svg), None, 1).cairo
I use PNGSurface because it renders the data to a ImageSurface. The PNG functionality is not used here.
Unfortunately cairosvg uses cairocffi instead of pycairo. That's why I changed my whole project to cairocffi. That required only adopting the imports and one minor change.

XNA curve import from Maya?

I am trying to import a movement curve from Maya into my XNA game, but I cannot figure out how. Basically I want to catch the curve by it's name, and look up its values at different points of time.
Are curves exported into FBX at all? And, if not, then how to catch it?
Edit: Maya can export to Maya ASCII, and I tried to parse it, but I am not sure what formula I should use to recreate the curve.
Here is a Maya ASCII segment defining a typical curve:
createNode transform -name "curve1";
createNode nurbsCurve -name "curveShape1" -parent "curve1";
setAttr -keyable off ".visibility";
setAttr ".cached" -type "nurbsCurve"
3 11 0 no 3
16 0 0 0 1 2 3 4 5 6 7 8 9 10 11 11 11
14
-4.9774564508407968 0 -6.8331005825440476
-5.5957526204336077 0 -5.5944567905896161
-6.8323449596191823 0 -3.1171692066807277
-5.6935230034445992 0 3.3047128765440847
-1.6528787527978079 0 8.8676235621397499
7.5595909161095838 0 10.325347443191644
9.2297347448508607 0 8.5586791722955731
10.0730315036276 0 0.93412333819133941
5.9770106513247976 0 3.7809964481624871
2.9006817236214149 0 -3.3327711853359037
11.373191256465434 0 -4.6672854260704906
4.5697574985247682 0 -14.178349348937205
2.4191279569332935 0 -11.415532638650156
1.3438131861375628 0 -10.034124283506653
;
I managed to find the file format reference somewhere, the important info here is the knot indexes (16 0 0 0 1 2 3 4 5 6 7 8 9 10 11 11 11) and the coordinates (all lines containing three numbers).
But, I still have no idea how to recreate the curve. I googled a lot for nurbscurves, bsplines etc, but could not successfully match the result in Maya with any code I could find.
I've achieved this in 3dsmax by exporting the curve in Ascii format and parsing the text manually, does Maya have any such exporter?

Resources