Sharp: Color picker

Created on 5 Sep 2017  路  3Comments  路  Source: lovell/sharp

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?

cookbook question

Most helpful comment

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];

All 3 comments

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];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

janaz picture janaz  路  3Comments

vermin1337 picture vermin1337  路  3Comments

genifycom picture genifycom  路  3Comments

paulieo10 picture paulieo10  路  3Comments

DuckerMan picture DuckerMan  路  3Comments