Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 channel grey+alpha, resize with non-opaque background: vector must have 1 or 3 elements #2569

Closed
banli17 opened this issue Feb 9, 2021 · 7 comments

Comments

@banli17
Copy link

banli17 commented Feb 9, 2021

I used gm to convert the shadowed text into a picture, and then resize the picture with sharp resize, and it reported an error.

nodejs.unhandledRejectionError: linear: vector must have 1 or 3 elements

I tried to modify the color of the shadow and found that sometimes an error is reported, such as when the shadow is #000000. When I change to #040303, no error will be reported.

When I removed the background of sharp resize, I found that no error was reported. But I need transparent text images. Here is the code:

// buf is the png buffer generated by gm
const input = await sharp(buf);
this.imageBuffer = await input.resize({
             width: options.width,
             height: options.height,
             // kernel: sharp.kernel.nearest,
             fit: options.fit ||'contain',
             position: this.sharpPosition,
             background: {r: 0, g: 0, b: 0, alpha: 0 },
           })
             .png()
             .toBuffer();
@lovell
Copy link
Owner

lovell commented Feb 9, 2021

Please can you provide a sample input image and standalone code (e.g. no undefined variables) to allow someone else to reproduce this problem.

@banli17
Copy link
Author

banli17 commented Feb 21, 2021

@lovell

This is the picture I generated with gm, I don’t know why I get an error when I use sharp。

When I remove the resize background, it doesn’t report an error

image

sharp('./axxx.png')
    .resize({
        width: 120,
        height: 30,
        // kernel: sharp.kernel.nearest,
        fit: 'contain',
        background: {r: 0, g: 0, b: 0, alpha: 0},
    })
.png().toBuffer()

image

@lovell
Copy link
Owner

lovell commented Feb 21, 2021

I cannot reproduce this on Linux using the latest sharp v0.27.1 with the input image and sample code provided.

I downloaded the sample axxx.png image as follows:

$ curl -so axxx.png https://user-images.githubusercontent.com/12556069/108620148-020f2d00-7465-11eb-8ca6-70fce9b41757.png

It might be that GitHub is altering the image. Please can you provide a standalone repo with a package.json, sample input and code that allows someone else to reproduce this.

@banli17
Copy link
Author

banli17 commented Feb 21, 2021

year ,it seems github is altering the image, you can try https://github.com/banli17/sharp_test

and i use globally-installed libvips v8.10.0

When the color of fill stoke is .fill('#000000') .stroke('#333333'), an error will be reported, and sometimes no error will be reported, such as fill('red')

image

@lovell
Copy link
Owner

lovell commented Feb 21, 2021

Thank you, I can now reproduce this. The input image is 16-bit, 2 channel (grey+alpha) and it fails when the background has a non-opaque alpha channel. I'll investigate further, thanks for reporting this.

@lovell lovell added bug and removed question labels Feb 21, 2021
@lovell lovell changed the title nodejs.unhandledRejectionError: linear: vector must have 1 or 3 elements 2 channel grey+alpha, resize with non-opaque background: vector must have 1 or 3 elements Feb 21, 2021
@lovell
Copy link
Owner

lovell commented Feb 22, 2021

This problem relates to sharp's alpha detection, which doesn't currently support 16-bit, 2 channel input. Luckily libvips now provides/exposes a much better has_alpha feature so commit cc37b59 switches to use that instead.

@lovell lovell added this to the v0.27.2 milestone Feb 22, 2021
@lovell
Copy link
Owner

lovell commented Feb 22, 2021

v0.27.2 now available with the fix.

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

No branches or pull requests

2 participants