Node.js 中的图像处理

Manipulation of an Image in Node.js

我想将图像的方向更改为从纵向到横向,反之亦然。

我尝试使用许多可用的 npm 包来完成它:
* rotate-image
* image-rotate
* jpegorientation
* gm
* rad

但是这些方法中的每一种都无法像我想的那样工作,或者每种方法都有一些或其他的限制。
我试图找到一个可能的解决方案并尽可能多地搜索它。 大多数时候,它是使用 gm 完成的。 我尝试使用 gm 进行图像处理:

Code:

var gm = require('gm');
gm('path/filename.jpg')
    .autoOrient()
   .write('path/destinationfile.jpg', function (err) {
        if (err) {
            console.log(err);
        } else
            console.log("Orientation changed successfully!");
    })  

但是我收到一个错误:

Error: Could not execute GraphicsMagick/ImageMagick: gm "identify" "-ping" "-verbose" "path/filename.jpg" this most likely means the gm/convert binaries can't be found.

这个错误只能通过使用外部二进制来解决libraries.I不想涉及任何类型的第三方库的依赖。
有人可以帮我解决如何无需预安装任何外部二进制库(brew 等)或如何以更好的方式使用任何现有包。

您可以使用 contain method from lwip - 用于 NodeJS 的轻量级图像处理器

这是一个例子:

// obtain an image object:
require('lwip').open('Example.jpg', function(err, image){
  // check err...
  // define a batch of manipulations and save to disk as JPEG:
  image.batch()
    .contain(image.height(), image.width(), 'black')
    .writeFile('output.jpg', function(err){
      // check err...
      console.log(err || 'Done');
    });
});

这会交换方向并用黑色填充空白。

我刚刚试过了,我只需要将 lwip 模块安装到我的应用程序中。

npm install lwip --save

很有可能是(CentOS 的)PATH 问题。所以我会试试这个:

sudo ln -s /usr/local/bin/gm /usr/bin/gm
sudo ln -s /usr/local/bin/convert /usr/bin/convert

希望对您有所帮助。

$ sudo apt-get install graphicsmagick