使用 Node Js 将普通图像转换为渐进图像
Convert Normal Image into Progressive Image using Node Js
有什么方法可以编写 API 将普通图像转换为 Progressive Using Nodejs。我不想使用 Base64。
我在这上面做了很多研发。我发现的一个图书馆,即 lib turbo-jpeg
但只有他们在解码图像。我也想对该图像进行编码。
请查看下方 link 以供参考。如果有人有任何解决方案,请告诉我。
Link 是 -
https://github.com/LinusU/cwasm-jpeg-turbo
请帮我写出 API 将图像转换为渐进式。
提前致谢。
您可以使用 graphicsmagick with gm
module 将支持的图像格式转换为渐进式 JPEG。
这是一个简单的示例(在 macOS 上使用 GraphicsMagick v1.3.31 和 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();
它下载 this 非渐进式 JPEG 图像并将其转换为渐进式 JPEG 图像并将其作为 progressive.jpg
保存在与脚本相同的目录中。
如果有人和我一样 c/p @fardjad 的回答并遇到以下错误,对我来说 (Ubuntu 16.04) 只是改变了一行:
(node:14862) UnhandledPromiseRejectionWarning: Error: Could not execute GraphicsMagick/ImageMagick: gm "identify" "-ping" "-verbose" "-" this most likely means the gm/convert binaries can't be found
发件人:
const gm = require('gm'); // https://www.npmjs.com/package/gm
收件人:
const gm = require('gm').subClass({imageMagick: true});
有什么方法可以编写 API 将普通图像转换为 Progressive Using Nodejs。我不想使用 Base64。 我在这上面做了很多研发。我发现的一个图书馆,即 lib turbo-jpeg 但只有他们在解码图像。我也想对该图像进行编码。 请查看下方 link 以供参考。如果有人有任何解决方案,请告诉我。
Link 是 - https://github.com/LinusU/cwasm-jpeg-turbo
请帮我写出 API 将图像转换为渐进式。
提前致谢。
您可以使用 graphicsmagick with gm
module 将支持的图像格式转换为渐进式 JPEG。
这是一个简单的示例(在 macOS 上使用 GraphicsMagick v1.3.31 和 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();
它下载 this 非渐进式 JPEG 图像并将其转换为渐进式 JPEG 图像并将其作为 progressive.jpg
保存在与脚本相同的目录中。
如果有人和我一样 c/p @fardjad 的回答并遇到以下错误,对我来说 (Ubuntu 16.04) 只是改变了一行:
(node:14862) UnhandledPromiseRejectionWarning: Error: Could not execute GraphicsMagick/ImageMagick: gm "identify" "-ping" "-verbose" "-" this most likely means the gm/convert binaries can't be found
发件人:
const gm = require('gm'); // https://www.npmjs.com/package/gm
收件人:
const gm = require('gm').subClass({imageMagick: true});