pycairo icon indicating copy to clipboard operation
pycairo copied to clipboard

Getting rasterized pixels

Open zumpchke opened this issue 4 years ago • 3 comments

Let's say I draw a bezier curve using curve_to and then stroke. Is there a way to return the pixels that were changed by this operation?

zumpchke avatar Aug 09 '20 04:08 zumpchke

Whats the use-case ? I don't believe it is directly possible (though on a low level, cairos span-compositor may heve the information). It may be possible to work out a slightly different approach to achieve what you want.

Here is a slightly inefficient approach that may work:

  • Use stroke-extents to get the area that will be painted.
  • Create a greyscale bitmap* the size of that area
  • Paint the path onto in black
  • Check which pixels are non-white

*Making the intermediate bitmap 8bit greyscale, or 1 bit black and white should help performance here.

stuaxo avatar Oct 05 '20 22:10 stuaxo

I'm running an optimization algorithm which needs to calculate the mean square difference between two images along the path of a line. I have implemented your solution as a workaround.

I was hoping for an API similar to skimage.

>>> img = np.zeros((10, 10), dtype=np.uint8)
>>> rr, cc = bezier_curve(1, 5, 5, -2, 8, 8, 2)
>>> img[rr, cc] = 1

zumpchke avatar Oct 05 '20 23:10 zumpchke

Suggestion: implement your own Bézier rendering algorithm. The De Casteljau construction is not hard to implement.

ldo avatar Aug 04 '21 07:08 ldo