-
-
Notifications
You must be signed in to change notification settings - Fork 110
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
[Feature] webp support #44
Comments
how can we do that? |
Well, how to you currently convert images to arrays of pixels ? |
In browser, it's done by node-vibrant is designed to be extendable. Image format support is provided via To implement one, one could Output pixel array should be the same format as It's really just a matter of finding a webp decoder package for node. |
Great! thanks! I am not that good in TypeScript, but I'll see what can be done! |
Nice! FYI. I'm refactoring node-vibrant into multiple small packages for version 3.1.0. All image classes are implemented as their own npm packages. Check out |
Great ! |
I've been using this package in my Discord bot which is starting to move to displaying all its images in webp format by default due to its many advantages so this feature is starting to be a huge thing for me. I see a @nitriques have you done any of your "seeing what can be done" yet? or are you going to work on it after all @akfish ? |
@favna It won't be fixed in the main package/repo. But it is fixable by implementing a custom ImageClass (and published as a separate npm package). WebP libs for node.js (that I've seen) require external binaries or native modules, which could be messy to support across platforms. The main package is intended to work out-of-box for all platforms. I'm working on this project in my spare time and this feature is not high on my priority list. So I'm afraid it won't be fixed by me any time soon. |
Yes and all I see are native ad-dons The good thing is that I need it, so I might have the time to poke around it soon. |
How do you feel about allowing to use sharp instead of jimp via an optional flag? Since v0.20 sharp doesn't require any additional installation steps besides |
I'm just trying to create a sharp-based |
fyi: Here's a ImageClass implementation using sharp. It supports both webp and svg in addition to all the default formats. Due to the resizing issue described in the comment above, the code resizes the image in the import * as sharp from "sharp";
import {ImageBase, ImageSource} from "@vibrant/image";
class SharpImage extends ImageBase {
private _image: ImageData = undefined as any;
async load(image: ImageSource): Promise<ImageBase> {
if (typeof image === "string" || image instanceof Buffer) {
const {data, info} = await sharp(image)
.resize(200, 200, {fit: "inside", withoutEnlargement: true})
.ensureAlpha()
.raw()
.toBuffer({resolveWithObject: true});
this._image = {
width: info.width,
height: info.height,
data: (data as unknown) as Uint8ClampedArray,
};
return this;
} else {
return Promise.reject(
new Error("Cannot load image from HTMLImageElement in node environment")
);
}
}
clear(): void {}
update(): void {}
getWidth(): number {
return this._image.width;
}
getHeight(): number {
return this._image.height;
}
resize(targetWidth: number, targetHeight: number, ratio: number): void {
// done in the load step, ignoring any maxDimension or quality options
}
getPixelCount(): number {
const {width, height} = this._image;
return width * height;
}
getImageData(): ImageData {
return this._image;
}
remove(): void {}
} |
@danielberndt that code snippet is so good, thanks for it 🙂 is it any reason you are resizing the image first? I think that keeping the image as larger as possible is going to improve the result. |
@Kikobeats if I remember correctly, I copied the default behaviour of this library which also decreased the size of the image before processing it. Resizing the image before processing leads to more consistent (and much shorter) processing times. |
Hum, I tried to find the old code as reference but no success – maybe @akfish @rutchcorn can confirm is resize is a thing done internally these days or not really? btw, I adopt he code into a NPM high level library: microlinkhq/splashy#15 |
@Kikobeats, @danielberndt is correct in terms of faster (and lower resource usage) processing when resizing. However, because the image is now smaller and the image quantizer now inherently has less data, the output colors will be less accurate (altho, FWIW "accurate" + "quantizer" is a... Dicey proposition and idk if we'll ever truly get there so long as I'm involved at any stage lol) Honestly, I'd go with resizing first. There's very little reason to demand high color accuracy where it would matter, and if it did - this library would already not provide your needs |
Here's the place where the resize is triggered: Line 52 in 8843d00
|
is webp support in progress? |
I know it is not an easy piece of work, but offering webp support would be pretty cool.
Thanks.
The text was updated successfully, but these errors were encountered: