使用JIMP着色
我正在使用JIMP从文件夹中着色png图像,但是我会遇到错误:W和H必须是数字(第42行 - image.Color函数。)。这似乎应该是一个简单的操作,但是我发现的解决方案非常复杂。似乎JIMP是要走的路,但是显然它有一些我不熟悉的怪癖。
const { jimpEvChange } = require('@jimp/core');
const { write } = require('jimp');
const { composite } = require('jimp');
const jimp = require('jimp');
var fs = require('fs');
// create an array of 6 colors and specify the colors
const colors = [
['green'],
['red'],
['blue'],
['yellow'],
['purple'],
['orange']
];
// call functions to colorize the images
var pngFiles = GetPNGs("ToColor/");
for (var i = 0; i < pngFiles.length; i++) {
var image = new jimp(pngFiles[i]);
Colorize(image, colors[i]);
image.write(pngFiles[i]);
}
// get pngs from a folder "ToColor" and colorize them each using the colors array
function GetPNGs (folder) {
var pngFiles = [];
const newLocal = fs.readdirSync(folder);
var files = newLocal;
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (file.split(".").pop() == "png") {
pngFiles.push(folder + "/" + file);
}
}
return pngFiles;
}
// colorize the images
function Colorize (image, color) {
image.color([
{ apply: 'red', params: [color[0]] },
{ apply: 'green', params: [color[0]] },
{ apply: 'blue', params: [color[0]] }
]);
}
// loop through the images and colorize them
function ColorizeImages (pngs, colors) {
for (var i = 0; i < pngs.length; i++) {
var image = new jimp(pngs[i]);
Colorize(image, colors[i]);
image.write(pngs[i]);
}
}
任何提示都将不胜感激。 谢谢, 詹姆斯。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对此做了一个破解,并提出了这个示例:
请注意,此代码需要在带有
.mjs
扩展的文件中,因为我们使用import> import
语句而不是需要
。您可以使用.mjs
文件与node inde index.mjs
的文件完全相同。如果您真的想使用需要
,请使用.js
extension将导入更改为需要并命名文件。您的代码遇到了多个问题。关于阅读实际图像和有关使用JIMP库的许多问题存在一些问题,但是除非您要我,否则我不会经历所有图像。
您对JIMP文档是对的,但是...很糟糕。尤其是如果您一般与JavaScript有点新秀。
您最大的问题可能是您尝试创建新的JIMP图像对象的方式。 文档说使用
new Jimp(... )
用于创建新的图像,这意味着如果您首先没有任何图像,则可以使用它。但是,当您已经将图像放在某个文件夹中并想加载它们以使用JIMP编辑时,您需要使用
jimp.read(...)
而不是使用。jimp.read
是一个异步函数,这意味着即使尚未读取图像,代码的其余部分也将继续运行。因此,我们需要使用等待jimp.read
,您可以想到“暂停”程序,直到jimp.read
实际上读取了图像。在读取图像并将图像对象放入名为
jimpimage
的变量之后,我们将使用预定义操作 jimpimage.color()
>,在这种情况下,我们使用Mix
。此功能不是异步的,因此我们不必等待
它。最后,在将着色操作应用于图像之后,我们通过使用
writeasync
将图像保存到具有相同名称(和可选后缀)的指定输出文件夹。这是一个异步函数,因为它的名称暗示了它,因此我们必须等待
it。程序运行后,您可以在指定的输出文件夹中找到修改的图像。
另请注意,JIMP将其中的一些文档委托,尤其是关于“颜色的东西”, tinycolor github github page 由于JIMP在引擎盖下使用TinyColor作为某些相关的实用程序。因此,如果您想知道是否可以使用“红色”一词,而不是“#ff0000”,那么TinyColor文档就有答案。
关于
错误:W和H必须是数字
-Error;最有可能的原因是您用var image = new Jimp(pngfiles [i]);
初始化了JIMP错误的图像。就像我说的那样,这是用于从头开始创建新图像,我指的是再次说,如果您使用此语法来创建新图像,则将其使用(其中前两个参数为 width 和 height ,这不是在您的代码中给出的):我为您提供了一个简化的示例,说明了如何读取图像,将某些操作应用于它们,然后将修改后的图像写回某些文件夹。我将其余的留给你!
请问您是否有任何疑问,我现在是JIMP大师。
这些是我使用的测试图像:
/oe6xy.png“ rel =” nofollow noreferrer“>
Allright I took a crack at it and came up with this example:
Note that this code needs to be in a file with
.mjs
extension, because we're usingimport
statements instead ofrequire
. You can run.mjs
file exactly the same way than normal.js
files withnode index.mjs
. If you really want to userequires
instead, change the imports to requires and name the file normally with.js
extension.Your code had, well, multiple problems. There was some issues regarding reading the actual images and a multitude of issues regarding using the Jimp library, but I am not going to go through all of them unless you want me to.
You are right though about the Jimp documentations, it's... awful. Especially if you are somewhat rookie with JavaScript in general.
You biggest issue was probably how you tried to create new Jimp image objects. The documentation says that using
new Jimp(...)
is for creating new images, which means that you would use it if you had no images anywhere in the first place.However, when you already have your images in some folder and want to load them up to edit with Jimp, you need to use
jimp.read(...)
instead.jimp.read
is an asynchronous function, which means that the rest of your code will continue running even if the image hasn't been read yet. For this reason we need to useawait jimp.read
which you could think of like "pausing" the program untiljimp.read
has actually read the image.After the image has been read and the image object lies into a variable named
jimpImage
, we calljimpImage.color()
with the array of predefinedoperations
, in this case we're usingmix
. This function is not asynchronous, so we don't have toawait
it.Finally after we've applied the coloring operations to the image, we save the image to the specified output folder with the same name (and optional suffix) by using
writeAsync
. This is an asynchronous function as the name of it implies, so we have toawait
it.After the program has finished running, you can find your modified images in the specified output folder.
Also note that Jimp delegates some of the documentation, especially regarding "color stuff", to TinyColor Github page, as Jimp uses TinyColor under the hood for certain color related utility stuff. So if you're wondering if you can use the word "red" instead of "#FF0000" for example, TinyColor documentation has the answer for that.
Regarding the
error: w and h must be numbers
-error; most likely cause for it was that you initialized the images wrong for Jimp withvar image = new jimp(pngFiles[i]);
. Like I said, this is for creating new images from scratch and I refer to the documentation again, which says that if you ARE using this syntax to create new images, it is used like this (where the first two parameters are the width and height, which were not given in your code):I've given you a simplified example of how to read images, apply some operations to them and write the modified images back to some folder. I'll leave the rest to you!
Do ask if you have any questions, I'm a Jimp master now.
These are the test images I used:
And these are what the program output (remember the amount is only 60 and our base images have strong colors):