Does sharp support colorpicker like imagemagick?
equivalent of something like:
coords="20,20"
color=`convert someimage.jpg -format "%[pixel:s.p{$coords}]" info:`
convert -size 100x100 xc:"$color" background.jpg
I want to get color for each corner. Is it possible with Sharp?
Hello, you can access raw pixel data as follows:
const coords = { x: 20, y: 20 };
sharp('someimage.jpg')
.raw()
.toBuffer()
.then(data => {
// data is a Buffer of length (width * height * channels)
// containing 8-bit RGB(A) pixel data.
});
@lovell great! How do I access color data from within .then? I want to use that colour and apply that colour on the background
For a JPEG input there will be 3 channels in the output.
data[0] is the value of red at 0, 0
data[1] is the value of green at 0, 0
data[2] is the value of blue at 0, 0
data[3] is the value of red at 1, 0
etc.
You should be able to calculate the offset into the buffer of an x, y point using something like
const offset = channels * (width * y + x);
const red = data[offset];
const green = data[offset + 1];
const blue = data[offset + 2];
Most helpful comment
For a JPEG input there will be 3 channels in the output.
data[0]is the value of red at0, 0data[1]is the value of green at0, 0data[2]is the value of blue at0, 0data[3]is the value of red at1, 0etc.
You should be able to calculate the offset into the buffer of an
x, ypoint using something like