使用Node Js将普通图像转换为渐进图像 [英] Convert Normal Image into Progressive Image using Node Js

查看:87
本文介绍了使用Node Js将普通图像转换为渐进图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何编写API的方法,可将普通图像转换为使用Node.js的渐进式图像.我不想使用Base64. 我已经做了很多R& D就此.我发现的一个库,即lib turbo-jpeg 但这只是他们在解码图像.我也想对该图像进行编码. 请检查以下链接以供参考.如果有人有任何解决方案,请告诉我.

Is there any way to write API which will convert normal image into Progressive Using Nodejs. I don't want to use Base64. I have done so much R & D on this. One library i found i.e lib turbo-jpeg But in this only they are decoding the image .I want to encode that image too. Please check this below link for reference. If anyone got any solution then please let me know.

链接为- https://github.com/LinusU/cwasm-jpeg-turbo

请帮助我编写API,以将图像转换为令人满意的图像.

Please help me out to write API to convert image into progreesive.

谢谢.

推荐答案

您可以使用的="tag"> graphicsmagick 来转换支持的图像格式到渐进式JPEG.

You can use graphicsmagick with gm module to convert supported image formats to progressive JPEG.

这是一个简单的示例(已在具有GraphicsMagick v1.3.31和Node.js v10.15.3的macOS上进行了测试):

Here's a simple example (tested on macOS with GraphicsMagick v1.3.31 and Node.js v10.15.3):

const fs = require('fs');
const path = require('path');
const https = require('https');

const gm = require('gm'); // https://www.npmjs.com/package/gm

const gmIdentify = gmInstance =>
  new Promise((resolve, reject) => {
    gmInstance.identify((err, data) => {
      if (err) {
        reject(err);
        return;
      }
      resolve(data);
    });
  });

const gmStream = (gmInstance, writeStream, format = 'jpg') =>
  new Promise((resolve, reject) => {
    gmInstance
      .stream(format)
      .pipe(writeStream)
      .once('finish', resolve)
      .once('error', reject);
  });

// This function downloads the file from the specified `url` and writes it to
// the passed `writeStream`.
const fetchFile = (url, writeStream) =>
  new Promise((resolve, reject) => {
    https.get(url, res => {
      if (res.statusCode !== 200) {
        reject(new Error('Something went wrong!'));
        return;
      }

      res
        .pipe(writeStream)
        .once('finish', () => {
          resolve();
        })
        .once('error', err => {
          reject(err);
        });
    });
  });

const main = async () => {
  // Download a sample non-progressive JPEG image from the internet
  const originalJpegUrl =
    'https://upload.wikimedia.org/wikipedia/commons/b/b4/JPEG_example_JPG_RIP_100.jpg';
  const originalPath = path.join(__dirname, './original.jpeg');
  await fetchFile(originalJpegUrl, fs.createWriteStream(originalPath));
  originalJpegData = await gmIdentify(gm(fs.createReadStream(originalPath)));
  console.log(
    `Interlace for the original jpeg file is set to ${
      originalJpegData['Interlace']
    }`,
  );

  // Convert the downloaded file to Progressive-JPEG
  const progressiveJpegPath = path.join(__dirname, 'progressive.jpg');
  await gmStream(
    gm(fs.createReadStream(originalPath))
      .interlace('line' /* or 'plane' */) // https://www.imagemagick.org/MagickStudio/Interlace.html
      .quality(originalJpegData['JPEG-Quality']), // save with the same quality as the original file
    fs.createWriteStream(progressiveJpegPath),
  );
  const progressiveJpegData = await gmIdentify(
    gm(fs.createReadStream(progressiveJpegPath)),
  );
  console.log(
    `Interlace for the converted jpeg file is set to ${
      progressiveJpegData['Interlace']
    }`,
  );
};

main();

它下载非渐进式JPEG图像和将其转换为渐进式JPEG图像,并将其另存为progressive.jpg与脚本在同一目录中.

It downloads this non-progressive JPEG image and converts it to a progressive JPEG image and saves it as progressive.jpg in the same directory as the script.

这篇关于使用Node Js将普通图像转换为渐进图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆