编解码器
编解码器 允许您使用读取、写出、编辑和分析图像.
npm install @astropub/codecs
使用量
import * as fs from 'node:fs/promises'
import * as codecs from '@astropub/codecs'
// load the JPG image
const image = await codecs.load(
await fs.readFile('./kitten.jpg')
)
image.type // string representing the image type ('image/jpeg')
image.data // Uint8Array representing the image data
image.width // number representing the image width
image.height // number representing the image height
image.ext // string representing the image extension ('jpg')
const decoded = await image.decode()
decoded.data // Uint8ClampedArray representing the decoded image data
decoded.width // number representing the decoded image width
decoded.height // number representing the decoded image height
// encode the image as Avif & WebP, at 320, 640, & 960
for (const size of [ 320, 640, 960 ]) {
const resized = await decoded.resize({ width: size })
for (const type of [ 'image/avif', 'image/webp' ]) {
const encoded = await resized.encode(type, { quality: 80 })
encoded.type // string representing the encoded image type ('image/webp')
encoded.data // Uint8Array representing the encoded image data
encoded.width // number representing the encoded image width (320 | 640 | 960)
encoded.height // number representing the encoded image height
encoded.ext // string representing the encoded image extension ('webp')
await fs.writeFile(`./kitten-${size}.${encoded.ext}`, encoded.data)
}
}
API 密码
load
这个 load 函数返回已装入的图像。 它接受一个字符串路径,文件 URL,缓冲,响应,或打入箭头.
const image = await codecs.load('./kitten.jpg')
image.type // string representing the image type ('image/jpeg')
image.data // Uint8Array representing the image data
image.width // number representing the image width
image.height // number representing the image height
image.ext // string representing the image extension ('jpg')
decode
这个 decode 函数返回已解码的图像。 它接受缓冲或打字箭头.
const buffer = await fs.readFile('./kitten.jpg')
const decoded = await codecs.decode(buffer)
decoded.data // Uint8ClampedArray representing the decoded image data
decoded.width // number representing the decoded image width
decoded.height // number representing the decoded image height
个人解码器可供 avif, (中文(简体) ), jpg, (中文(简体) ), jxl, (中文(简体) ), png, (中文(简体) ), webp,以及 wp2。 。 。 .
import * as codecs from '@astropub/codecs'
codecs.avif.decode(await fs.readFile('./kitten.avif'))
codecs.jpg.decode(await fs.readFile('./kitten.jpg'))
codecs.jxl.decode(await fs.readFile('./kitten.jxl'))
codecs.png.decode(await fs.readFile('./kitten.png'))
codecs.webp.decode(await fs.readFile('./kitten.webp'))
codecs.wp2.decode(await fs.readFile('./kitten.wp2'))
encode
这个 encode 函数返回已编码的图像。 它接受解码图像.
const encodedImage = await codecs.encode(decoded, 'image/webp', { quality: 80 })
encoded.type // string representing the encoded image type ('image/webp')
encoded.data // Uint8Array representing the encoded image data
encoded.width // number representing the encoded image width (320 | 640 | 960)
encoded.height // number representing the encoded image height
encoded.ext // string representing the encoded image extension ('webp')
await fs.writeFile('./kitten.webp', encodedImage)
个人编码器可供 avif, (中文(简体) ), jpg, (中文(简体) ), jxl, (中文(简体) ), png, (中文(简体) ), webp,以及 wp2。 。 。 .
import * as codecs from '@astropub/codecs'
codecs.avif.encode(decoded)
codecs.jpg.encode(decoded)
codecs.jxl.encode(decoded)
codecs.png.encode(decoded)
codecs.webp.encode(decoded)
codecs.wp2.encode(decoded)
resize
这个 resize 函数返回已调整大小的图像。 它接受解码图像.
const resized = await codecs.resize(decoded, { width: 320 })
resized.data // Uint8ClampedArray representing the resized image data
resized.width // number representing the resized image width
resized.height // number representing the resized image height
如果没有指定,则调整大小 height 将从 width 使用公式 width / naturalWidth * naturalHeight。 。 。 .
blur
这个 blur 函数返回模糊的图像。 它接受解码图像.
const blurred = await codecs.blur(decoded, { radius: 30 })
blurhash
这个 blurhash 函数返回模糊的图像,使用 Wolt BlurHash 算法它接受解码图像.
const blurhashed = await decoded.blurhash({ width: 32 })
如未具体说明 height 将使用公式从图像宽度中确定 width / naturalWidth * naturalHeight。 。 。 .
type
这个 type 函数返回图像缓冲器的内容类型。 它接受缓冲或打字箭头.
// 'image/jpeg'
const type = await codecs.type(buffer)
ext
这个 ext 函数返回图像缓冲器的文件扩展名。 它接受缓冲或打字箭头.
// 'jpg'
const ext = await codecs.ext(buffer)
DecodedImage
这个 DecodedImage 类代表原始的已解码的图像数据.
const decoded = new DecodedImage(
data // Uint8ClampedArray
width // number
height // number
)
DecodedImage#encode
这个 encode 函数 DecodedImage 返回当前已解码的图像中承诺的编码图像.
const encoded = await decoded.encoded('image/webp') // EncodedImage<'image/web', Uint8Array>
DecodedImage#blur
这个 blur 函数 DecodedImage 返回当前已解码的图像中承诺的模糊图像.
const blurred = await decoded.blur({ radius: 30 }) // DecodedImage
DecodedImage#blurhash
这个 blurhash 函数 DecodedImage 返回当前已解码图像中承诺的模糊图像.
const blurhash = await decoded.blurhash({ radius: 30 }) // DecodedImage
DecodedImage#resize
这个 resize 函数 DecodedImage 返回当前已解码的图像中承诺的调整图像.
const resized = await decoded.resize({ width: 320 }) // DecodedImage
DecodedImage#color
这个 color 财产 DecodedImage 返回已解码图像中的主要颜色.
decoded.color // [ 57, 52, 43 ]
EncodedImage
这个 EncodedImage 类代表分析,编码图像数据.
const encoded = new EncodedImage(
type // string ('image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2')
data // Uint8Array
width // number
height // number
)
EncodedImage#decode
这个 decode 函数 EncodedImage 从当前编码的图像返回已承诺的已解码的图像.
const decoded = await encoded.decoded()
类型
ImageType
这个 ImageType 类型代表已知的图像内容类型.
import type { ImageType } from '@astropub/codecs'
// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType
ImageType
这个 ImageType 类型代表已知的图像内容类型.
import type { ImageType } from '@astropub/codecs'
// 'image/avif' | 'image/gif' | 'image/jpeg' | 'image/jxl' | 'image/png' | 'image/svg+xml' | 'image/webp' | 'image/webp2'
ImageType
许可证
编解码器 通常是一个重混 斯库什!。 。 。 .
该项目的原始代码根据CC0-1.0许可证颁发.
代码从 斯库什! 依据Apache-2.0许可,版权Google Inc.
代码从 Avif 编码器 依据BSD许可,版权为Joe Drago.
代码从 莫兹杰普格 根据修改后的(3-clause)BSD许可,版权为Viktor Szathmáry.
代码从 联 合 国 依据Apache-2.0许可,版权Google Inc.
代码从 氧化金 根据麻省理工学院的许可,版权为Joshua Holmer.
代码从 网络P 由Modific (3-clause) BSD License,版权:Google Inc.授权使用.
代码从 网络P2 依据Apache-2.0许可,版权Google Inc.
代码从 模糊 获得麻省理工学院许可,版权为Olli Mahlamäki.