Get bounding box of SVG drawing with Python? - python

I would like to extract the bounding box of a SVG drawing.
Since Python is already available on the system and also used to perform other tasks, I don't want to use JavaScript or any other language. My understanding is if the bounding box of a single element can be calculated (but I don't know how).
The bounding box of the whole drawing is just the minimum and maximum x,y values a of all elements, hence probably the bounding boxes of all elements need to be calculated.
I am a Python beginner, but svgwrite is probably not the right module and so far I was scared by the installation of rsvg on a Windows system.
Thank you for any hints and pointing me to the right direction.

Related

Coordinates of framed text on an image

I would like to get the coordinates of framed text on an image. The paragraphs have thin black borders. The rest of the image contains usual paragraphs and sketchs.
Here is an example:
Do you have any idea of what kind of algorithms should I use in Python with an image library to achieve this ? Thanks.
A few ideas to detect a framed text which largely comes down to searching boxes/rectangles of substantial size:
find contours with OpenCV, analyze shapes using cv2.approxPolyDP() polygon approximation algorithm (also known as Ramer–Douglas–Peucker algorithm). You could additionally check the aspect ratio of the bounding box to make sure the shape is a rectangle as well as check the page width as this seems to be a known metric in your case. PyImageSearch did this amazing article:
OpenCV shape detection
in a related question, there is also a suggestion to look into Hough Lines to detect a horizontal line, taking a turn a detecting vertical lines the same way. Not 100% sure how reliable this approach would be.
Once you find the box frames, the next step would be to check if there is any text inside them. Detecting text is a broader problem in general and there are many ways of doing it, here are a few examples:
apply EAST text detector
PixelLink
tesseract (e.g. via pytesseract) but not sure if this would not have too many false positives
if it is a simpler case of boxes being empty or not, you could check for average pixel values inside - e.g. with cv2.countNonZero(). Examples:
How to identify empty rectangle using OpenCV
Count the black pixels using OpenCV
Additional references:
ideas on quadrangle/rectangle detection using convolutional neural networks

How do I get Flood Filled area on python?

There is a library on python does flood fill, the PIL.ImageDraw.floodfill function colors some part of the image using Flood Fill algorithm.
I want to write python application that allows me to write some memo about specific area on the map. The map input will be an image, if you click an area on the map, a window will pop up so you can write a memo about the area.
The problem occurs when you try to get memos written about particular area. I want to implement the program to show all memos related to clicked area.
To do this, I decided to save memos with its coordinate information so I can select it with flood fill algorithm. in order to do this, I have to flood fill to select the area, not color the area.
The term select means watching some area so you can decide whether or not a coordinate is inside of the area.
So what should I do to select the area? are there any further improvements on my program design?
I was able to detect contours with this post solution. Then we can just traverse all contours list and check if click coords is in some polygon.

Connect close/overlapping detected bounding boxes

I'm working with text detection in Tensorflow, using EAST: An Efficient and Accurate Scene Text Detector.
And I'm struggling to find any decent/library that helps grouping close and overlapping bounding boxes like in the example:
I think it's a simple problem, there must be some OpenCV/Tensorflow function for this that I don't know yet.
Any ideas?

Draw extruded 3d text with pyopengl?

What is the best way to draw 3d text with pyopengl, preferably with modern opengl ?
There seem to be quite a few examples, but mostly in old-style opengl and not for python
First you need a text outline which requires a font.
On windows you can use WinGDI fonts by creating a dummy context, selecting a created font on it and getting outlines of every character. For Truetype you load glyphs into the face and simply access their outlines.
For the front & back face of your 3D text, you need to convert the outlines/contours to triangles. Either use the good old GLU tesselator, the relative new tesselation shader of OpenGL 4 or a geometry library like CGAL. Then simply draw the triangles for the front and again with some additional depth for the back.
For the sides you simply span rectangles behind outline segments/lines with height equal to your wished text depth - these are your text sides. You can simply use a depth vector and transform everything afterwards or calculate the orthogonal vector with 2 segments.
You notice that this all can be expensive, so don't hesitate to cache as much as possible.
I referenced to C sources because i'm more familiar with these, but i bet there're python equivalents to port my explanations.

How to get an ROI based tooltip in Python + any imaging library

Running Python, I have an image and some data calculated for different ROIs (regions of interest).
I would like to display that image, and have a tooltip pop up whenever I am over one of those regions of interest.
This is mainly for debugging purposes - so I don't care that things will be very pretty, or integrate into any other sort of GUI - just that I can easily understand what value I calculated for each part of the image.
Also - I don't mind which imaging/display library to use for that purpose. I normally work with PIL, or directly with numpy arrays - but other libraries are just as good for me.
Thanks!
If it's for debugging you can simply get the position of mouse clicks and print the value for the corresponding ROI. I would use OpenCV as it has SetMouseCallback() and you can define ROIs by polygons and then test what polygon gets the click, see this example. If you've never used OpenCV before then maybe this is not the best option.

Categories