Sharp: "Error: linear: vector must have 1 or 2 elements" raised when resizing a b&w png image

Created on 14 Apr 2016  路  3Comments  路  Source: lovell/sharp

Hi Lovell,

Sharp is failing to resize a png file with the following metadata:

{ format: 'png',
  width: 1653,
  height: 2339,
  space: 'b-w',
  channels: 1,
  density: 200,
  hasProfile: false,
  hasAlpha: false }

To reproduce, please run the following:

var sharp = require('sharp');
var i = sharp('image.png');

i.embed() // works without embed
  .resize(100, 100)
  .background({r:0,g:0,b:0,a:0}) // works with a:1
  .toBuffer(function(err, output, info) {
    if (err) {
      throw err;
    } else {
      console.log("All good")
    }
  });

The error thrown is Error: linear: vector must have 1 or 2 elements

It works when 'embed()' is not used or the alpha channel in the background color is set to 1

I created a repo that showcases the issue and includes the b&w png file: https://github.com/janaz/sharp-png-issue

bug

Most helpful comment

v0.14.1 now available via npm. Please re-open this if it doesn't solve the problem Tomasz.

All 3 comments

That looks like a bug - the background colour embedding logic has probably never been tested with 1 channel images - I'll take a look.

Commit e0d5826 adds support for embed with 1 and 2 channel images. Thanks for reporting this!

v0.14.1 now available via npm. Please re-open this if it doesn't solve the problem Tomasz.

Was this page helpful?
0 / 5 - 0 ratings