pycairo icon indicating copy to clipboard operation
pycairo copied to clipboard

How to use with `numpy`

Open page200 opened this issue 2 years ago • 3 comments

Here in tests/test_surface_numpy.py, shape should be (h, w) rather than (w, h). It currenlty works correctly only because w and h are equal.

An older version of this code used to be shape=(h, w, 4), dtype=np.uint8, that seemed clearer for cairo.FORMAT_RGBA32 and cairo.FORMAT_RGB24 images than shape=(h, w), dtype=numpy.uint32

The filename of that script is mentioned somewhere in the documentation as far as I recall, but not linked.

Overall, it would be nice to provide examples how to "unpack" the bits (for example in the case of cairo.FORMAT_RGB30), so that individual users don't have to code that (with np.right_shift etc.) every time like here:
https://community.mypaint.org/t/color-management-and-linear-light/944/17

Usage with numpy seems quite important, but quite hidden.

page200 avatar Nov 06 '22 23:11 page200

We have some docs here: https://pycairo.readthedocs.io/en/latest/integration.html

I'm fine with creating a separate sub-page there for specific libraries and providing more examples there.

lazka avatar Nov 11 '22 14:11 lazka

Here in tests/test_surface_numpy.py, shape should be (h, w) rather than (w, h). It currenlty works correctly only because w and h are equal.

fixed in 809a93cc8

An older version of this code used to be shape=(h, w, 4), dtype=np.uint8, that seemed clearer for cairo.FORMAT_RGBA32 and cairo.FORMAT_RGB24 images than shape=(h, w), dtype=numpy.uint32

Would that work with cairo using native-endian there?

lazka avatar Nov 29 '22 06:11 lazka

I'm fine with creating a separate sub-page there for specific libraries and providing more examples there.

I started this now in 8e2ef6f171a318

lazka avatar Nov 29 '22 07:11 lazka