You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Enhancement: ensure metadata.background is compatible with color package for single-channel images #4090
When using sharp(input).metadata() an object with properties, which includes background is returned.
background: Default background colour, if present, for PNG (bKGD) and GIF images, either an RGB Object or a single greyscale value
It can return RGB Object or greyscale number.
When using sharp(input).flatten() we can pass in an options object which includes the background color property. This option expects an RGB Object or a CSS type color string (e.g. 'white').
What I did, was use the background color returned by .metadata(), directly as an option in .flatten(). This however, produced an error in cases where the metadata returned a greyscale number, which is not a valid input for flatten background option.
Current workaround is simple: just repeat the greyscale number value for all the RGB Object fields.
constimage=sharp(input)constdata=awaitimage.metadata();// THIS CAN FAIL: when background is greyscale numberconstimage.flatten({background: data.background??'white'})// include fallback when no background is returned.toBuffer();// WORKAROUND:constbackground=typeofdata.background==='object'
? data.background
: {r: data.background,g: data.background,b: data.background};constimage.flatten({ background })// need a separate check for fallback (not included here).toBuffer();
However, while the "fix" is simple, it is not the expected behavior and should be adjusted. I propose that the .flatten()background option accepts a greyscale number as possible input.
The simplest solution here is probably to always return an RGB Object in metadata.background regardless of the number of input channels.
Happy to accept a PR for this, if you're able. We should add a test case for this too as it doesn't appear to be covered by any of the scenarios in test/unit/metadata.js at the moment.
I'm OK with this being a breaking change, as part of a future possible v0.34.0, as it simplifies the API so that the background metadata can always be parsed by the color package and therefore operations that use it such as flatten().
Perhaps a single-channel input could return something like { gray: value } instead? The color package accepts values in the range 0-100 so we would need to scale from libvips' 0-255 range.
changed the title [-]Enhancement: ensure metadata.background always returns RGB Object[/-][+]Enhancement: ensure metadata.background is compatible with color package for single-channel images[/+]on Apr 30, 2024
Activity
lovell commentedon Apr 29, 2024
The simplest solution here is probably to always return an RGB Object in
metadata.backgroundregardless of the number of input channels.Happy to accept a PR for this, if you're able. We should add a test case for this too as it doesn't appear to be covered by any of the scenarios in test/unit/metadata.js at the moment.
[-].flatten() not accepting background greyscale options[/-][+]Enhancement: ensure metadata.background always returns RGB Object[/+]Zirafnik commentedon Apr 29, 2024
Removing greyscale number return type from
.metadata()would be a breaking change for anybody that confidently relies on receiving just the number.Adding additional parameter type for
.flatten()background to include a number, is not a breaking change.lovell commentedon Apr 29, 2024
I'm OK with this being a breaking change, as part of a future possible v0.34.0, as it simplifies the API so that the
backgroundmetadata can always be parsed by thecolorpackage and therefore operations that use it such asflatten().Perhaps a single-channel input could return something like
{ gray: value }instead? Thecolorpackage accepts values in the range 0-100 so we would need to scale from libvips' 0-255 range.[-]Enhancement: ensure metadata.background always returns RGB Object[/-][+]Enhancement: ensure metadata.background is compatible with color package for single-channel images[/+]Ensure background metadata can be parsed #4090
lovell commentedon Apr 4, 2025
sharp v0.34.0 now available with this improvement, thanks for the original report.
Ensure background metadata can be parsed lovell#4090