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
It would be great if the opacity/alpha of a sharp image instance could be directly changed,
e.g. making it semitransparent, putting out as raw to buffer and overlaying another image with it.
(Also useful for visually debugging overlay compositions).
Hello, a possible workaround for this might be to overlay with a raw pixel buffer containing tiled, semi-transparent pixel value, something like the following (untested):
Hello, [0, 0, 0, 128] was an example of adding a semi-transparent alpha channel. If you want it to be fully-transparent, you can use [0, 0, 0, 0].
If you know the image dimensions then an alternative method of adding a fully-transparent alpha channel is by passing a zero-filled Buffer to joinChannel, for example (untested):
It would be more user friendly to just have a dedicated method since joinChannel is so cumbersome and overlaying four channels on the image seems like a very high performance cost for such a simple task.
let img = await sharp(file).addAlpha();
await img.png().toFile(...);
Reacted by strarsis, Nikhil, Nikita Savchenko and Joe Federer
Why even make it that verbose though? If someone is adding an alpha channel to an image it must be for the entire image. You can't selectively make one part of the image RGB format and another RGBA format.
Activity
[-]Change opacity[/-][+]Change opacity/alpha[/+]lovell commentedon Sep 2, 2016
Hello, a possible workaround for this might be to overlay with a raw pixel buffer containing tiled, semi-transparent pixel value, something like the following (untested):
strarsis commentedon Sep 2, 2016
Thanks. Though an API method could be more intuitive.
strarsis commentedon Sep 14, 2016
The code works insofar that it adds an alpha channel -
but the output image is now different compared to the input image, much darker.
When I remove tile: true there will be still one pixel changed -
but the output image should be identical to input image plus an alpha channel.
lovell commentedon Sep 14, 2016
Hello,
[0, 0, 0, 128]was an example of adding a semi-transparent alpha channel. If you want it to be fully-transparent, you can use[0, 0, 0, 0].If you know the image dimensions then an alternative method of adding a fully-transparent alpha channel is by passing a zero-filled Buffer to joinChannel, for example (untested):
quinton-ashley commentedon Mar 7, 2018
This is a whole lot of code for simply adding an alpha channel to the images.
It would be more user friendly to just have a dedicated method since joinChannel is so cumbersome and overlaying four channels on the image seems like a very high performance cost for such a simple task.
lovell commentedon Mar 8, 2018
Rather than dealing with raw data and Buffer allocation, something like the following might be a bit simpler (
createwas added in v0.17.3).EDIT: this won't work as the
createfeature only supports 3 and 4 channels.quinton-ashley commentedon Mar 8, 2018
Why even make it that verbose though? If someone is adding an alpha channel to an image it must be for the entire image. You can't selectively make one part of the image RGB format and another RGBA format.
lovell commentedon Mar 9, 2018
@quinton-ashley I've created #1153 for this suggestion; PRs always welcome.
slavafomin commentedon Nov 19, 2018
Hello!
I was also looking for a simple method to change the image transparency, but couldn't find one.
Why is this issue closed and why is it classified as a question? I think it's a perfectly valid feature request. @lovell
I would really like to see this in the library. Thank you for your hard work.
lovell commentedon Nov 19, 2018
@slavafomin This feature request is being tracked at #618