imagemagick-native中文文档|imagemagick-native js中文教程|解析

npm npmdoc 2年前 (2022-01-02) 966次浏览

imagemagick-native中文文档|imagemagick-native js中文教程|解析

安装命令:npm i imagemagick-native

节点图像魔术本机

ImageMagick用于NodeMagick++绑定

特征

  • 本地绑定到 C/C++ Magick++ 库
  • 异步、同步和流 API
  • 支持convertidentifycomposite,等实用功能

构建状态

目录

例子

转换格式

通过质量控制从一种格式转换为另一种格式:

fs.writeFileSync('after.png', imagemagick.convert({
    srcData: fs.readFileSync('before.jpg'),
    format: 'PNG',
    quality: 100 // (best) to 1 (worst)
}));

原始JPEG:

替代文字

转换为 PNG:

质量 100 质量 50 质量 1
替代文字 替代文字 替代文字

图片由David Yu 提供

模糊

模糊图像:

fs.writeFileSync('after.jpg', imagemagick.convert({
    srcData: fs.readFileSync('before.jpg'),
    blur: 5
}));

替代文字 变成 替代文字

图片由Tambako The Jaguar 提供

调整大小

通过指定width和 来调整图像大小height共有三种调整大小样式:

  • aspectfill: 默认。生成的图像将恰好是指定的大小,并且可能会被裁剪。
  • aspectfit:缩放图像,使其不必被裁剪。
  • fill:挤压或拉伸图像,使其完全填充指定的大小。
fs.writeFileSync('after_resize.jpg', imagemagick.convert({
    srcData: fs.readFileSync('before_resize.jpg'),
    width: 100,
    height: 100,
    resizeStyle: 'aspectfill', // is the default, or 'aspectfit' or 'fill'
    gravity: 'Center' // optional: position crop area when using 'aspectfill'
}));

原来的:

替代文字

调整大小:

纵横比 方面适合 充满
替代文字 替代文字 替代文字

图片由克里斯托夫提供

旋转、翻转和镜像

旋转和翻转图像,并将两者结合起来进行镜像:

fs.writeFileSync('after_rotateflip.jpg', imagemagick.convert({
    srcData: fs.readFileSync('before_rotateflip.jpg'),
    rotate: 180,
    flip: true
}));

原来的:

替代文字

修改的:

旋转 90 度 旋转 180 度 翻动 翻转 + 旋转 180 度 = 镜像
替代文字 替代文字 替代文字 替代文字

图片由比尔·格雷西提供

API 参考

转换(选项,[回调])

转换提供的缓冲区options.srcData并返回缓冲区。

options参数可以具有以下值:

{
    srcData:        required. Buffer with binary image data
    srcFormat:      optional. force source format if not detected (e.g. 'ICO'), one of http://www.imagemagick.org/script/formats.php
    quality:        optional. 1-100 integer, default 75. JPEG/MIFF/PNG compression level.
    trim:           optional. default: false. trims edges that are the background color.
    trimFuzz:       optional. [0-1) float, default 0. trimmed color distance to edge color, 0 is exact.
    width:          optional. px.
    height:         optional. px.
    density         optional. Integer dpi value to convert
    resizeStyle:    optional. default: 'aspectfill'. can be 'aspectfit', 'fill'
                    aspectfill: keep aspect ratio, get the exact provided size.
                    aspectfit:  keep aspect ratio, get maximum image that fits inside provided size
                    fill:       forget aspect ratio, get the exact provided size
    gravity:        optional. default: 'Center'. used to position the crop area when resizeStyle is 'aspectfill'
                              can be 'NorthWest', 'North', 'NorthEast', 'West',
                              'Center', 'East', 'SouthWest', 'South', 'SouthEast', 'None'
    format:         optional. output format, ex: 'JPEG'. see below for candidates
    filter:         optional. resize filter. ex: 'Lagrange', 'Lanczos'.  see below for candidates
    blur:           optional. ex: 0.8
    strip:          optional. default: false. strips comments out from image.
    rotate:         optional. degrees.
    flip:           optional. vertical flip, true or false.
    debug:          optional. true or false
    ignoreWarnings: optional. true or false
}

笔记

  • format值可以在这里找到
  • filter值可以在这里找到

callback可以提供一个可选参数,在这种情况下convert将异步运行。完成后,callback将使用错误和结果缓冲区调用:

imagemagick.convert({
    // options
}, function (err, buffer) {
    // check err, use buffer
});

还有一个流版本:

fs.createReadStream('input.png').pipe(imagemagick.streams.convert({
    // options
})).pipe(fs.createWriteStream('output.png'));

识别(选项,[回调])

标识提供的缓冲区srcData并返回一个对象。

options参数可以具有以下值:

{
    srcData:        required. Buffer with binary image data
    debug:          optional. true or false
    ignoreWarnings: optional. true or false
}

callback可以提供一个可选参数,在这种情况下identify将异步运行。完成后,callback将使用错误和结果对象调用:

imagemagick.identify({
    // options
}, function (err, result) {
    // check err, use result
});

该方法返回一个类似于以下内容的对象:

{
    format: 'JPEG',
    width: 3904,
    height: 2622,
    depth: 8,
    density : {
        width : 300,
        height : 300
    },
    exif: {
        orientation: 0 // if none exists or e.g. 3 (portrait iPad pictures)
    }
}

quantizeColors(选项)

从提供的缓冲区中将图像量化为指定数量的颜色srcData并返回一个数组。

options参数可以具有以下值:

{
    srcData:        required. Buffer with binary image data
    colors:         required. number of colors to extract, defaults to 5
    debug:          optional. true or false
    ignoreWarnings: optional. true or false
}

该方法返回一个类似于以下内容的数组:

[
    {
        r: 83,
        g: 56,
        b: 35,
        hex: '533823'
    },
    {
        r: 149,
        g: 110,
        b: 73,
        hex: '956e49'
    },
    {
        r: 165,
        g: 141,
        b: 111,
        hex: 'a58d6f'
    }
]

复合(选项,[回调])

将提供options.compositeData的缓冲区options.srcData与由指定的重力提供的缓冲区组合在一起,options.gravity并返回一个缓冲区。

options参数可以具有以下值:

{
    srcData:        required. Buffer with binary image data
    compositeData:  required. Buffer with binary image data
    gravity:        optional. Can be one of 'CenterGravity' 'EastGravity' 'ForgetGravity' 'NorthEastGravity' 'NorthGravity' 'NorthWestGravity' 'SouthEastGravity' 'SouthGravity' 'SouthWestGravity' 'WestGravity'
    debug:          optional. true or false
    ignoreWarnings: optional. true or false
}

callback可以提供一个可选参数,在这种情况下composite将异步运行。完成后,callback将使用错误和结果缓冲区调用:

imagemagick.composite(options, function (err, buffer) {
    // check err, use buffer
});

这个库目前只提供这些,如果你想要更多,请尝试node-imagemagick

getConstPixels(选项)

获取提供的矩形区域的像素。

options参数可以具有以下值:

{
    srcData:        required. Buffer with binary image data
    x:              required. x,y,columns,rows provide the area of interest.
    y:              required.
    columns:        required.
    rows:           required.
}

用法示例:

// retrieve first pixel of image
var pixels = imagemagick.getConstPixels({
    srcData: imageBuffer, // white image
    x: 0,
    y: 0,
    columns: 1,
    rows: 1
});

返回:

[ { red: 65535, green: 65535, blue: 65535, opacity: 65535 } ]

每个颜色值的大小都是imagemagick.quantumDepth位。

量子深度

返回 ImageMagick 的 QuantumDepth,它是在编译时定义的。

例如:16

版本

以字符串形式返回 ImageMagick 的版本。

例如:’6.7.7′

安装

Linux / Mac OS X

安装ImageMagick的与安装该模块前头。在 CentOS 6 和 Mac OS X Lion、Ubuntu 12.04 上使用 ImageMagick 6.7.7 进行测试。

brew install imagemagick

  or

sudo yum install ImageMagick-c++ ImageMagick-c++-devel

  or

sudo apt-get install libmagick++-dev

确保您可以在 PATH 中找到 Magick++-config。然后:

npm install imagemagick-native

安装说明

  • RHEL/CentOS:如果node-imagemagick-native官方 RPM 库中没有提供所需的 ImageMagick版本,请尝试-lastLes RPM de Remi 提供版本,例如:
sudo yum remove -y ImageMagick
sudo yum install -y http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo yum install -y --enablerepo=remi ImageMagick-last-c++-devel
  • Mac OS X:您可能需要先安装pkgconfig
brew install pkgconfig

视窗

在 Windows 7 x64 上测试。

  1. 仅安装 Python 2.7.3 2.7.3 没有其他工作很正常!

    如果您使用 Cygwin,请确保您没有在 Cygwin 安装程序中安装 Python,因为对于使用哪个版本会有一些混淆。

  2. 安装Visual Studio C++ 2010 Express

  3. (仅限 64 位)安装 Windows 7 64 位 SDK

  4. 安装Imagemagick dll binary x86 Q16Imagemagick dll binary x64 Q16,在安装过程中检查库和包含。

然后:

npm install imagemagick-native

性能 – 简单的缩略图创建

imagemagick:       16.09ms per iteration
imagemagick-native: 0.89ms per iteration

详情请参阅node test/benchmark.js

注意: node-imagemagick-native的主要优点是它直接使用 ImageMagick 的 API,而不是通过执行其命令行工具之一。这意味着当在库中花费的时间很少时它会快得多,否则会更少。有关讨论,请参阅问题 #46

贡献

该项目遵循“OPEN Open Source”理念。如果您提交拉取请求并合并它,您很可能会获得对该存储库的提交访问权限。

许可证(麻省理工学院)

版权所有 (c) Masakazu Ohtsuka http://maaash.jp/

特此授予任何人免费获得本软件和相关文档文件(“软件”)副本的许可,不受限制地处理本软件,包括但不限于使用、复制、修改、合并的权利、发布、分发、再许可和/或出售软件的副本,并允许向其提供软件的人员这样做,但须符合以下条件:

上述版权声明和本许可声明应包含在本软件的所有副本或重要部分中。

该软件“按原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于适销性、特定用途的适用性和不侵权的保证。在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任承担责任,无论是在合同诉讼、侵权行为或其他方面,由软件或软件的使用或使用或其他原因引起的或与之相关的软件。

项目贡献人员列表:


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:imagemagick-native中文文档|imagemagick-native js中文教程|解析
喜欢 (0)
.excerpt .focus {display:none}