Skip to content

How to handle premultiplied images in JS #44

@skycoop

Description

@skycoop

I'm using sharp to resize the images for use with thumbhash with user uploaded images like so

const { data, info } = await sharp(image)
  .resize({ width: 100, height: 100, fit: "inside" })
  .ensureAlpha()
  .raw()
  .toBuffer({ resolveWithObject: true });

console.log(info)
/*
  {
    format: 'raw',
    width: 100,
    height: 100,
    channels: 4,
    depth: 'uchar',
    premultiplied: true,
    size: 40000
  }
*/

const thumbhash = rgbaToThumbHash(info.width, info.height, data);

I noticed that sharp is outputting premultiplied images depending on the import format (PNG always seems to be premultiplied, webp isn't, etc.). I can't figure out a way to disable premultiplication or unpremultiply using sharp, so I was wondering if there was a way to handle premultiplied images within rgbaToThumbHash or process it myself before passing it to rgbaToThumbHash?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions