- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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]);
}
}
如有任何提示,我们将不胜感激。谢谢,詹姆斯。
最佳答案
好吧,我试了一下,想出了这个例子:
请注意,此代码需要位于扩展名为 .mjs
的文件中,因为我们使用的是 import
语句而不是 require
。您可以使用与使用 node index.mjs
的普通 .js
文件完全相同的方式运行 .mjs
文件。如果您真的想改用 requires
,请将导入更改为 requires 并使用 .js
扩展名正常命名文件。
import jimp from "jimp";
import fs from "fs";
// I wanted to make this example to use async/await properly with Jimp
// So that's why we are using util.promisify to convert fs.readdir
// into a function named readDir, which we can await on
import util from "util";
const readDir = util.promisify(fs.readdir);
// Colors for mix operations
const colors = [
{r: 0, g: 255, b: 154, a: 1},
{r: 255, g: 40, b: 108, a: 1},
{r: 26, g: 172, b: 255, a: 1},
{r: 255, g: 190, b: 171, a: 1},
{r: 255, g: 239, b: 117, a: 1},
{r: 137, g: 91, b: 255, a: 1}
];
// Colorsnames for output file naming, these correspond to colors array
const colorNames = ['green', 'red', 'blue', 'orange', 'yellow', 'purple'];
// Define which color operations we want to do, using mix as an example
// https://www.npmjs.com/package/jimp#colour-manipulation
const operations = colors.map((c) => {
return { apply: "mix", params: [c, 60 ]};
});
// Input and output folder names
const inputFolderName = "./ToColor";
const outputolderName = "./out";
const outputFileSuffix = "edited"; // Optional suffix for the output files
// We're using async/await, so must wrap top level code like this
// https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level
(async () => {
// Get filenames of the png files in the specified folder
let pngFileNames = await readDir(inputFolderName);
// Optional filtering of only .png files
pngFileNames = pngFileNames.filter((f) => f.includes(".png"));
// Go through each file
// Must use for...of loop here, because we have awaits inside the loop
let i = 0;
for (let fileName of pngFileNames) {
// Optional output file name suffixing
const outPutFileName = outputFileSuffix.length > 0 ? fileName.split('.').reduce((a, b) => `${a}_${outputFileSuffix}.${b}`) : fileName;
// Make an actual Jimp image object from the file
const jimpImage = await jimp.read(`${inputFolderName}/${fileName}`);
// Make one new image per operation, so in total, we output colors.length * pngFileNames.length images
let j = 0;
for(let colorOperation of operations) {
// Apply operation
jimpImage.color([colorOperation]);
// Write the edited image to out folder
await jimpImage.writeAsync(`${outputolderName}/${colorNames[j]}_${outPutFileName}`);
j++;
}
i++;
}
})();
您的代码有很多问题。在阅读实际图像方面存在一些问题,在使用 Jimp 库方面存在大量问题,但除非您希望我这样做,否则我不会一一详述。
关于 Jimp 文档,您是对的,它……太糟糕了。特别是如果您总体上是 JavaScript 新手。
您最大的问题可能是您如何尝试创建新的 Jimp 图像对象。 The documentation说使用 new Jimp(...)
是为了创建新图像,这意味着如果您首先没有任何图像,您将使用它。
但是,当您已经在某个文件夹中拥有图像并想加载它们以使用 Jimp 进行编辑时,您需要改用 jimp.read(...)
。 jimp.read
是一个异步函数,这意味着即使尚未读取图像,您的其余代码也会继续运行。出于这个原因,我们需要使用 await jimp.read
,您可以将其视为“暂停”程序,直到 jimp.read
实际读取图像。
在读取图像并将图像对象放入名为 jimpImage
的变量中后,我们使用预定义的 操作数组调用
,在本例中我们使用 jimpImage.color()
mix
。此函数不是异步的,因此我们不必await
。
最后,在我们对图像应用着色操作后,我们使用 writeAsync
将图像保存到具有相同名称(和可选后缀)的指定输出文件夹。顾名思义,这是一个异步函数,所以我们必须await
它。
程序运行完成后,您可以在指定的输出文件夹中找到修改后的图像。
另请注意,Jimp 将一些文档委托(delegate)给了 TinyColor Github page,尤其是关于“颜色内容”的文档,因为 Jimp 在引擎盖下使用 TinyColor 来处理某些与颜色相关的实用程序。因此,如果您想知道是否可以使用“红色”一词代替“#FF0000”,例如,TinyColor 文档提供了答案。
关于错误:w和h必须是数字
-错误;最可能的原因是您使用 var image = new jimp(pngFiles[i]);
为 Jimp 初始化了错误的图像。就像我说的,这是为了从头开始创建新图像,我指的是 the documentation再次,它表示如果您使用此语法来创建新图像,它会像这样使用(其中前两个参数是 width 和 height,它们不是在您的代码中给出):
new Jimp(256, 256, (err, image) => {
// this image is 256 x 256, every pixel is set to 0x00000000
});
我已经为您提供了一个简化示例,说明如何读取图像、对图像应用一些操作并将修改后的图像写回某个文件夹。剩下的就交给你了!
有问题就问吧,我现在是 Jimp 大师了。
这些是我使用的测试图像:
这些是程序输出的内容(记住数量只有 60 并且我们的基础图像具有强烈的颜色):
关于javascript - 使用 jimp 为图像着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72614669/
我必须在文本区域上绘制哪些选项? 我认识一些所见即所得的编辑器。我不确定他们使用的方法是什么,并且会看看他们以了解,但我知道像 tinymce 这样的事情已经存在了一段时间,所以我猜现在可能有更现代的
我在我的 woo commerce 商店页面中创建了一个子类别排序菜单。我想将链接到我现在所在页面的链接设置为红色。我对 css 没有问题,只是用 js 脚本。我怎样才能让它突出当前?这是我创建的排序
所以我使用 TextWrangler4.5.3 并主要使用 Python 编写脚本。当我编写 Python 时,我经常喜欢发表类似这样的评论:# BEGINS foo 然后是 # ENDS foo 我
我需要你的帮助! 我有一个表格,里面有行(姓名等)现在,当位于该行上的对象具有特定值时,我想为特定的 tableCells 背景着色。但我只得到它来读取这个单元格的值。但我需要阅读对象(在我的代码中称
有人知道在 uikit 中给 uiactionsheet 着色吗? 最佳答案 是的,因为它是一个 UIView(如 kmit 所述),您可以使用以下命令:addSubview,因此您可以添加自己的背景
在 vba 中,我想从工作表中的单元格中读取颜色来为条形图中的特定条形着色。我的问题是我的颜色格式为 4F9F92,但要在 makro 中读取它,我需要使用单元格中的 excel 颜色代码 40762
我正在尝试绘制一个 geom_histogram,其中条形图由渐变着色。 这就是我想要做的: library(ggplot2) set.seed(1) df <- data.frame(id=past
我正在构建一个带有 shiny 的应用程序,并使用与 Shiny gallery ( http://shiny.rstudio.com/gallery/slider-bar-and-slider-ra
在 iOS 6 和 xcode 4 中,我有这个: https://dl.dropboxusercontent.com/u/60718318/photo.PNG使用代码可以轻松创建 [editButt
我正在向我的 iPhone UI 添加自定义按钮,并希望使它们具有 Apple 应用程序中的玻璃外观。我有一个很好的默认玻璃图像,但我不想为我想要的每种色调(红色、绿色、蓝色等)都有一个单独的图像。
我到处寻找但没有找到解决方案。我有图像 1。如何以编程方式使用渐变对它们进行着色以获得图像 2 和 3?以下是这些图像: 我通过 Photoshop 应用到它们的色调是简单的 2 色线性渐变。 我的问
有没有办法告诉 OS X 自动设置 NSToolbarItem 的样式/色调? 我通过 IB/Xcode 添加了一个“图像工具栏项”,并将图标设置为黑色 PDF as described in the
我试图区分列范围图表上的多种类型的数据。问题是我希望能够按多个标准对它们进行分组。 我可以轻松地为不同的列着色,从而解决一个问题,但我希望能够根据我传递到图表的数据,为 xAxis 上的类别(图表是倒
有人在编译模式 Emacs 中添加了对 ansi-color 的支持吗?如果是这样,颜色写入程序必须检查什么属性/属性才能确保其事件终端支持 ANSI 转义着色。 最佳答案 已经有一个函数可以将颜色应
我正在尝试实现 Phong 着色模型,但我不确定如何选择光源。更具体地说,我不明白光源应该是什么样子。它应该是vec3吗?或者也许是我在 Java 代码中定义的矩阵?另外,如果我们假设我希望相机作为光
我有 2 个 dhtmlxSidebars,就像示例中一样 here如何为嵌套背景设置不同的背景颜色?如果我添加CSS .dhxsidebar_side { background-color:
我有一个 JTable,每行都根据最后一列中的值着色。 但是,当我单击标题对行进行排序时,颜色不会跟随行。 我尝试在 JTable 鼠标退出事件上调用我的“colourTable”方法(我知道 Hac
geom_hline 似乎忽略了美观,我错过了什么还是这是一个错误? df session 信息: R version 3.4.4 (2018-03-15) Platform: x86_64-pc-
这个问题已经有答案了: How to draw a BufferedImage with a color tint (1 个回答) 已关闭 8 年前。 如何将通过此处的图标着色为不同的颜色?假设我想拍
我想为除我点击的那个之外的所有 div 着色。在我的代码中它有效,但只有一次。如果我点击另一个 div,它不起作用。 http://jsfiddle.net/6VhK8/353/ id="1
我是一名优秀的程序员,十分优秀!