Easiest way to parse simple SVG into Python, applying all transforms - python

I'd like to draw a few simple objects in Inkscape (lines, circles, rectangles), group them, move them around, scale, rotate, make copies, etc. and then I need something which will let me load the SVG into Python and iterate over all these shapes, getting the relevant attributes (for circles: centre and radius; for lines: the two end-points; etc.).
I've installed and tried svg-utils, pysvg, svgfig and read about inkex, and while all of these seem to allow me to iterate through the XML structure, with varying degrees of awkwardness, as far as I can see none of them apply the transforms to the elements. So, if I draw a line from (0,0) to (1,1), group it, move it to (100,100), then its XML tag is still going to say (0,0) to (1,1), but its real position is computed by applying the transform in its containing group, to these end-points.
I don't want to write all this transform-application code myself, because that would be re-inventing the bicycle. But I need help finding a convenient existing bicycle...
One likely useful route is to find an exporter into a simple format, which would already have had to solve all these problems. Here is an example I found: http://en.wikipedia.org/wiki/SK1_%28program%29#Supported_formats
But which of the export formats listed there is likely to be the simplest?

Related

Splitting an image into overlapping tiles, and recording the tile name as the top left pixel coords from the image

I'm very new to image processing in Python (and not massively adept at python in general), so forgive me for how stupid this may sound. Im working with an AI for object detection, and need to submit 1000x1000 pixel images to it, that have been divided up from larger images of varying lengths and widths (not necessarily divisible, but I have a way of padding out images less than 1000x1000). In order for this to work, I need 200 pixel overlap on each segment or the AI will pick may miss objects.
I've tried a host of methods, and have either got the image to divide up using the methods suggested in Creating image tiles (m*n) of original image using Python and Numpy and how can I split a large image into small pieces in python (plus a few others that effectively do the same techniques in different words. I've been able to make a grid and get the tile names from this, using How to determine coordinate of grid elements of an image, however have not been able to get overlap to work in this, as I would then just tile it normally.
Basically what I'm saying is that I've found one way to cut the images up that works, and one way to get the tile coordinates, but I am utterly failing at putting it all together. Does anyone have any advice on what to do here?
So far I've not found a direct approach to my end goal online - and I've tried mucking around with different scripts (like the ones listed above), but feel like Im barking up totally the wrong tree.

Find the crop parameters from two images

Given two images - one a cropped (but not scaled) portion of the other, how can I find the crop parameters (i.e.: the x and y offsets and width/height)? The idea is to crop one image (screenshot) by hand, and then crop a lot more at the same points.
Ideally via imagemagick, but I am happy with any pseudo-code solution, or with Perl, Python, JavaScript (in order of preference)
I have thought of a brute-force approach (find the first pixel which is the same color, check the next, keep going until different, or move to the next). Before I go down this barabarous (and probably slow) route, I'd like to check for better ones.
Template matching can be used for the identification of smaller image within a larger image.
The following resource might be helpful. Please check it out
https://docs.opencv.org/4.5.2/d4/dc6/tutorial_py_template_matching.html

Automaticly inserting text into dxf file with python

I‘m trying to automatically insert text into dxf contours using python. I have a bulk of dxf files for lasercutting. Often we want to engrave the partnumber into the sheetmetal part.
My attempt is to make a square box Where the length and with equals the text height and width. After to Programm found a place where it is outside the innerconturs and inside the outer contour I want to fill the box with text. I tried to abstract the contours with a polygon and start checking if it fits or not, which kind of works ok. Not finish completely yet.
I wondered if there is some sort of library /tool that has this function because computing time is quite high atm before putting more afford into the Programm or anyone has an easier approach than mine.
The next release of ezdxf (v0.16 is in beta now), has a new text2path add-on, which uses Matplotlib to render text strings as path objects. This path objects can be placed as:
POLYLINE and SPLINE entities to preserves the smooth curves
flattened to POLYLINE or LWPOLYLINE entities, which consist only of straight lines
HATCH entities with or without spline edges
flatten the paths into simple vertices
There are some examples in the https://github.com/mozman/ezdxf/tree/master/examples/addons folder (*_to_path.py).
It is possible to transform the path objects by a Matrix44 transformation and there even exist a function to fit some paths into a box: fit_paths_into_box().
For additional questions, you can use the discussions board at github.

Matplotlib export text as line elements in Python

I have to build some rudimentary CAD Tool in Python based on matplotlib for handling the display of the content.
After all the parts have been put together, the whole layout shall be exported as line elements (basically just tuples of the start / end coordinates of the lines, e.g. [x1,y1,x2,y2]) and just points.
So far I have all the basic gemoetric stuff implemented, but I cannot figure out how to implement text properly. To be able to use different fonts etc. I want to use the text capabilities of matplotlib, but I can't find a way to export the text properly from matplotlib.
Is there a way to get a vectorized output right away? Or at least an array of the plotted text?
After some days of struggling, I found a way to get the outline of the text: https://github.com/rougier/freetype-py , more precisely the example https://github.com/rougier/freetype-py/blob/master/examples/glyph-vector.py
If you just want to get the outline as an vector array, you can delete everything after line 78 and do this:
path = Path(VERTS, CODES)
outline = path.to_polygons()
This will give you an array of polygons, and each polygon is again an array of points (x,y) of the polygon.
Though it was some trouble to get freetype running on windows and I still have not figured out how to make it portable, I think I will stick with this solution, because it is fast, reliable and allows one to use all the nice system fonts.

Get absolute coordinates for rotation of Inkscape SVG elements

I am using wxPython to prototype some display interfaces for a project that will eventually become an embedded design. Since we are iterating with a graphics designer, it is much easier to let them work in a WYSIWYG editor like Inkscape than constantly rewrite 2D graphics drawing calls. For elements that need animation or dynamic text, the SVG elements are easily identifiable by ID so that they can be programatically updated before rendering. The rendering flow looks like this:
Inkscape SVG as XML --> Python modification of XML --> Drawing onto wxPython buffered canvas using Cairo and pyRSVG
I'd like to animate a pivoting needle around a fixed axis for a gauge. To define a needle, the graphics designer draws the graphical "needle" and a "needle_axis" element which is a small circle. Then, I thought I could set the transform property using the desired angle and the center coordinates of the "needle_axis." However, since the relative positions could have been affected because of other transforms, determining the absolute location of the rotation axis is non-trivial.
I cannot seem to find a Python library for determining absolute coordinates of an element, and it feels like reinventing the wheel to try an implement the part of the SVG spec. necessary to calculate it. The rsvg API mentions rsvg_handle_get_dimensions_sub and arsvg_handle_get_position_sub functions to get the dimensions and positions of elements, but I can't find an API for pyRSVG.
Is there a better way to find the rotation axis than manually calculating it? An example Inkscape SVG file and test snippet are available as a gist.
If I understood correctly, you have the needle's coordinates and the rotation axis coordinates, and you want to programatically change the needle's "angle" and then change the needle's shape in the SVG file accordingly?
I think this is a really bad idea: making changes to the SVG, saving and then rendering the "new SVG" will take too much ressources and is exactly why such a simple task seems complicated.
Instead, parse your SVG once (using lxml for instance, it's a piece of cake to extract coordinates just by doing some XPATH queries) and build your shapes into a model (a Needle class for instance, with the paths and center) and then simply rotate that needle using Cairo transforms. They already provide a rotate function that will do what you're looking for!

Categories

Resources