-1

I have vector graphics. (In my first case, it's the epigraph of a function whose formula is given. So it is a shape whose outline is given by a parametric curve.)

I want to rasterize this image with anti-aliasing. So I want raster graphics, i.e. a numpy array. I want to obtain this array in a low-level way, avoiding libraries that are meant for object-oriented interactive GUI visualizations with plot axes, etc.. I just want an array. The only problem with doing something like Y,X=np.ogrid(...) and then picture = Y>f(X) is that that's not anti-aliased. (Note that blurring that binary picture is worse than a good dedicated anti-aliasing algorithm.) How to rasterize with anti-aliasing in Python without any overkill GUI-centered libraries?

root
  • 1,812
  • 1
  • 12
  • 26
  • Consider looking up some tutorials, papers, and docs. The libraries you are trying to avoid may have valuable information on the subject. You can also do things like generate matplotlib figures as images, for example, without triggering a UI at all. – Mad Physicist Dec 21 '21 at 04:32
  • @MadPhysicist I guess you mean [`matplotlib` backends](https://matplotlib.org/stable/users/explain/backends.html). And maybe some low-level parts of `matplotlib` can be called that don't have the overhead of even considering to draw [anything (like spines)](https://matplotlib.org/stable/tutorials/introductory/usage.html#parts-of-a-figure) except the image itself, rather than consider it and decide to hide it or so. – root Dec 21 '21 at 10:30
  • Maybe a good lead: `Pycairo` (writing to a buffer and then from it to numpy is described [here](https://stackoverflow.com/a/58109664/5231110) and [here](https://github.com/dalembertian/pycairo/blob/master/test/isurface_get_data.py), maybe the buffer can be even used for some final goals directly without the detour through numpy) or `cairocffi`. Pros and cons of `Pycairo` vs. `cairocffi` are not quite clear. – root Mar 14 '22 at 00:50

2 Answers2

2

If the curve is given by an implicit equation F(x,y)=0, evaluate the value of the function at the four corners of every pixel. If the signs are the same, the pixel is wholly outside or inside. If the signs vary, the area inside the polygon formed by the corners and the points along the edges where the function vanishes (find these by a mere linear interpolation) tells you the mixture of background and foreground colors (alpha blending coefficient).

Tracing the polygon isn't that difficult: traverse the four edges of the square and keep the positive vertices and zero points in the order you meet them. You will get from a triangle to an hexagon. The area is obtained by the shoelace formula.

enter image description here


The case of a parametric function is a little harder. You need to find the intersections of the curve with the grid lines, and perform the area estimation in all cells that are traversed. For this, draw the curve as a polyline (this is called flattening), and slice the polyline with horizontals, then verticals.

  • Thank you! Are there libraries that do that? Using libraries is OK, just not something with unnecessary overhead like considering to draw [extra stuff such as spines and labels](https://matplotlib.org/stable/tutorials/introductory/usage.html#parts-of-a-figure) and then deciding for the option of hiding it. – root Dec 21 '21 at 10:31
  • 2
    @root: in such cases, I use my brain instead of a library. –  Dec 21 '21 at 13:38
0

Manim might be able to rasterize epigraphs and parametric curves well and fast. Its community edition seems actively maintained.

Edits/comments with details are welcome.

root
  • 1,812
  • 1
  • 12
  • 26