gpt4 book ai didi

图像序列的 Javascript 数组 - 仅返回序列中的第一张图像

转载 作者:行者123 更新时间:2023-11-28 05:37:30 24 4
gpt4 key购买 nike

我正在为 Adob​​e After Effects 开发 Extendscript 工具,并且正在尝试弄清楚一件事。 Extendscript 只是 Javascript,上面添加了一些 Adob​​e 绒毛,但几乎任何非浏览器特定的 Javascript 都可以工作。

我有一个脚本,我已经使用/开发了一段时间,它极大地简化了图像序列中数千个图像的导入。一切都运行良好,但当您开始处理数千张图像时,有一个特定区域运行速度非常慢,我正在尝试找出如何加快速度。

我试图优化的是遍历可能有数千个图像文件的数组,并将其连接到序列中的第一个图像。例如,我的数组可能是: [img.001.png、img.002.png、img.003.png、img.004.png、img.005.png] 我想要做的是删除“的每个实例img.###.png”,除了第一个。所以在我的函数之后我只会:[img.001.png]。

这是我现在的做法:
1)我取出数组项A,用正则表达式输出数字序列
2) 然后数组项B,正则表达式输出数字序列
3) 比较两者是否相同。如果是,我拼接出 B 项

这在 100% 的情况下有效,这很棒,但是当你获得超过一千或两张图像时,速度相当慢......我想做的是找到一种方法来更快地完成这个“trim ”步骤。我的一个 friend 告诉我正则表达式非常慢,所以也许我需要在没有正则表达式的情况下完成它?我还发现了一些可能有帮助的原生 JavaScript 数组方法,例如 forEach() every() 和 filter(),但我不知道这些方法会更快,因为我仍然需要进行 2 次正则表达式评估,而且我仍然需要对项目进行相互比较。

任何帮助将不胜感激!

  • 斯宾塞

这是我现有代码的片段:

currentFolder = new Folder ("//12.34.5.67/my folder on the network/")
var folderChildren = currentFolder.getFiles().sort();

var searcher = new RegExp("\\d{3,5}[.]");
for (var i = 0; i < folderChildren.length; i++) {
// Go through the array and strip out all elements that are the same once their numbers have been removed with a regex
if (i > 0) {
currentResult = searcher.exec(folderChildren[i].name); //check if a sequence
if (currentResult) { // it is a sequence
// first parse out the comparison strings - current item and item before
var testNameBefore = folderChildren[i-1].name;

//if we have a sequence before our current item, we need to delete the numbers.
var beforeNum = searcher.exec(testNameBefore);
if (beforeNum) {
testNameBefore = testNameBefore.substring(0, testNameBefore.length-8);
}

var testNameCurrent = folderChildren[i].name;
testNameCurrent = folderChildren[i].name.substring(0, testNameCurrent.length-8);

//compare to the element before it and delete if the same!!
if (testNameBefore == testNameCurrent) {
folderChildren.splice(i, 1);
i--;
}
}
}
}

最佳答案

您没有可比较的代码片段,但这只需几毫秒即可运行。

var files = getFiles(),
cleanedFiles = getDesequencedArray(files);

console.log(`Parsing ${files.length} files.`);
console.log(`Found ${cleanedFiles.length} unique sequences:`);
console.log(cleanedFiles);

function getDesequencedArray(files) {
return files.reduce(
function(memo, file) {
var sequence = file.match(/(.*)\.?\d{3,5}(.*)/);
if (sequence) {
if (!memo.found[sequence[1] + sequence[2]]) {
memo.found[sequence[1] + sequence[2]] = 1;
memo.files.push(file);
}
} else {
//Did you want the other files included too?
//memo.files.push(file);
}
return memo;
}, {
found: {},
files: []
}
).files;
}

function getFiles() {
var files = [
"img1.001.jpg",
"img1.002.jpg",

"img1.001.png",
"img1.002.png",
"img1.003.png",
"img1.004.png",

"img2.011.jpg",
"img2.012.jpg",
"img2.013.jpg",
"img2.014.jpg",

"img300001.png",
"img300002.png",
"img300003.png",
"img300004.png",

"img300002.100.png",
"img300002.200.png",

"readme.md",
"index.html"
];


// Adding a bunch of records
for (let x = 0; x < 100; x++)
for (
let i = 1;
i <= 1000;
files.push(`img-${x}.` + ("00" + i++).substr(-3) + '.png')
);
return files;
}

关于图像序列的 Javascript 数组 - 仅返回序列中的第一张图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39237646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com