Skip to content

[Question] How to manipulate channels? #680

Closed

Description

@btd

Hello. I could be missing something in docs, but i did not found how best to do such task.

I am trying at the end of pipeline to convert my image to rgba, if it is not (to use with ImageData and show in canvas).
So i literally copy pixel values depending number of channels (i do not make any color space conversion) in .toBuffer to produce new raw pixel values. Maybe there is a better way to do this?

Also could you please correct me if i wrong, i assume 1 channel it is grayscale image, 2 channels it is grayscale + alpha, 3 channels it is rgb and 4 it what i need rgba.

Activity

  1. lovell commented on Jan 12, 2017

    @lovell
    Owner

    Hello, forcing the addition of an alpha channel is not directly available via the existing API, but you could do something like the following (untested) which by extending 0 pixels using a fully transparent background should force your image to RGBA:

    sharp(input)
      .background({r: 0, g: 0, b: 0, alpha: 0})
      .extend({top: 0, bottom: 0, left: 0, right: 0})
      .raw()
      .toBuffer((err, data, info) => {
        const imageData = new ImageData(data, info.width, info.height);
        ...
      });
  2. btd commented on Jan 13, 2017

    @btd
    Author

    Thanks for answering.
    Nope, this does not work. I tried with usual jpg file with 3 channels, and it still remains with 3 channels.

  3. lovell commented on Jan 13, 2017

    @lovell
    Owner

    Ah, sorry, there's a > 0 check that prevents this approach. https://github.com/lovell/sharp/blob/master/src/pipeline.cc#L520 (I will update the parameter validation in the JavaScript to match the C++ logic).

    If you know the dimensions you could use an embedding resize:

    sharp(input)
      .background({r: 0, g: 0, b: 0, alpha: 0})
      .resize(width, height)
      .embed()
      .raw()
      .toBuffer((err, data, info) => {
        const imageData = new ImageData(data, info.width, info.height);
        ...
      });
  4. btd commented on Jan 13, 2017

    @btd
    Author

    Nope this still does not work. With the same jpg file i have tried:

    sharp(buffer)
          .raw()
          .toBuffer((err, data, info) => {
            if (err) {
              return ...;
            }
            
            sharp(data, { raw: info })
              .background({r: 0, g: 0, b: 0, alpha: 0})
              .resize(info.width, info.height)
              .embed()
              .raw()
              .toBuffer((err, data, info) => {
                 if (err) {
                   return ...;
                  }
    
               console.log(info); 
    /* prints
    { format: 'raw',
      width: 564,
      height: 752,
      channels: 3,
      size: 1272384 }
    */
           });
          });
  5. btd commented on Jan 13, 2017

    @btd
    Author

    I also would like to know also if it is possible to add own interpolator and reduction kernel. I have pretty specific usecase (pixel art) and generally good resampling methods not always suite there.

  6. lovell commented on Jan 15, 2017

    @lovell
    Owner

    Sorry, I'm not doing very well here. Third time lucky...

    For RGB input of known dimensions, the joinChannel operation with a zero-filled Buffer will add a fully-transparent alpha channel.

    const alpha = Buffer.alloc(width * height);
    sharp(rgb)
      .joinChannel(alpha, {
        raw: {
          width: width,
          height: height,
          channels: 1
        }
      })
      .raw()
      .then((rgba) => {
        ...
      });

    Modify the value of alpha Buffer to change the transparency, e.g. Buffer.alloc(width * height, 255) for fully-opaque.

  7. btd commented on Jan 16, 2017

    @btd
    Author

    Thanks, will try to use it for most common case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions