libvips / pyvips

python binding for libvips using cffi
MIT License
649 stars 50 forks source link

`addalpha` has different behaviour compared to C #457

Closed RiskoZoSlovenska closed 8 months ago

RiskoZoSlovenska commented 8 months ago

The following Python code:

import pyvips

img = pyvips.Image.black(1, 1).colourspace(pyvips.enums.Interpretation.SCRGB).addalpha()

print(".".join(str(pyvips.base.version(flag)) for flag in range(3)))
print(img.interpretation)
print(img.avg())

produces

8.15.1
scrgb
63.75

whereas the (hopefully) equivalent C code:

Click to open (this is my first time calling libvips from C — if I'm doing something wrong, please let me know!) ```c #include #include int main(int argc, char **argv) { if (VIPS_INIT(argv[0])) vips_error_exit(NULL); VipsImage *raw; if (vips_black(&raw, 1, 1, NULL)) vips_error_exit(NULL); VipsImage *scrgb; if (vips_colourspace(raw, &scrgb, VIPS_INTERPRETATION_scRGB, NULL)) vips_error_exit(NULL); g_object_unref(raw); VipsImage *with_alpha; if (vips_addalpha(scrgb, &with_alpha, NULL)) vips_error_exit(NULL); g_object_unref(scrgb); double mean; if (vips_avg(with_alpha, &mean, NULL)) vips_error_exit(NULL); printf("%s\n", VIPS_VERSION_STRING); printf("%d\n", vips_image_get_interpretation(with_alpha) == VIPS_INTERPRETATION_scRGB); printf("%f\n", mean); g_object_unref(with_alpha); return 0; } ```

produces

8.15.1
1
0.250000

This happens because pyvips re-implements the addalpha function, and this (re-)implementation wasn't updated when the upstream changed.

I'm opening this as an issue instead of directly PRing a fix because it may be worthwhile to consider using the libvips API directly so that issues like this can be avoided in the future.

jcupitt commented 8 months ago

Hello again,

I think the best solutions would be to either make vips_addalpha a true operation (and so skip the wrapping), or to fix the python implementation of this utility function. Calling into the C wrapper from the python wrapper would be fiddly and error-prone.

I pushed https://github.com/libvips/pyvips/commit/3c927624eda87d8e308b723ecbdd98823b1803cf and credited you.