gpt4 book ai didi

javascript - 使用 XMLHttpRequest 为 RGB 图像生成主色

转载 作者:数据小太阳 更新时间:2023-10-29 04:49:23 25 4
gpt4 key购买 nike

读者注意事项:这是一个很长的问题,但需要背景知识才能理解所提问题。

color quantization technique通常用于获取图像的主色。进行颜色量化的著名库之一是 Leptonica通过Modified Median Cut Quantization (MMCQ) and octree quantization (OQ)Github 的 Color-thief @lokesh 是 MMCQ 算法的一个非常简单的 JavaScript 实现:

var colorThief = new ColorThief();
colorThief.getColor(sourceImage);

从技术上讲,<img/> 上的图像HTML 元素支持 <canvas/>元素:

var CanvasImage = function (image) {
this.canvas = document.createElement('canvas');
this.context = this.canvas.getContext('2d');

document.body.appendChild(this.canvas);

this.width = this.canvas.width = image.width;
this.height = this.canvas.height = image.height;

this.context.drawImage(image, 0, 0, this.width, this.height);
};

这就是 TVML 的问题,我们稍后会看到。

我最近了解到的另一个实现链接在这篇文章 Using imagemagick, awk and kmeans to find dominant colors in images 上链接到 Using python to generate awesome linux desktop themes .作者发表了一篇关于 Using python and k-means to find the dominant colors in images 的文章那是在那里使用的(抱歉所有这些链接,但我正在追溯我的历史……)。

作者非常高效,还添加了一个 JavaScript 版本,我在这里发布:Using JavaScript and k-means to find the dominant colors in images

在这种情况下,我们生成图像的主色,不是使用 MMCQ(或 OQ)算法,而是使用 K-Means。问题是图像也必须是:

<canvas id="canvas" style="display: none;" width="200" height="200"></canvas>

然后

function analyze(img_elem) {
var ctx = document.getElementById('canvas').getContext('2d')
, img = new Image();
img.onload = function() {
var results = document.getElementById('results');
results.innerHTML = 'Waiting...';
var colors = process_image(img, ctx)
, p1 = document.getElementById('c1')
, p2 = document.getElementById('c2')
, p3 = document.getElementById('c3');
p1.style.backgroundColor = colors[0];
p2.style.backgroundColor = colors[1];
p3.style.backgroundColor = colors[2];
results.innerHTML = 'Done';
}
img.src = img_elem.src;
}

这是因为 Canvas 有一个 getContext() 方法,它公开了 2D 图像绘制 API - 参见 An introduction to the Canvas 2D API

这个上下文ctx被传递给图像处理函数

  function process_image(img, ctx) {
var points = [];
ctx.drawImage(img, 0, 0, 200, 200);
data = ctx.getImageData(0, 0, 200, 200).data;
for (var i = 0, l = data.length; i < l; i += 4) {
var r = data[i]
, g = data[i+1]
, b = data[i+2];
points.push([r, g, b]);
}
var results = kmeans(points, 3, 1)
, hex = [];
for (var i = 0; i < results.length; i++) {
hex.push(rgbToHex(results[i][0]));
}
return hex;
}

这样就可以通过Context在Canvas上绘制图像并获取图像数据了:

ctx.drawImage(img, 0, 0, 200, 200);
data = ctx.getImageData(0, 0, 200, 200).data;

另一个不错的解决方案是在 CoffeeScript 中,ColorTunes ,但这也使用了:

ColorTunes.getColorMap = function(canvas, sx, sy, w, h, nc) {
var index, indexBase, pdata, pixels, x, y, _i, _j, _ref, _ref1;
if (nc == null) {
nc = 8;
}
pdata = canvas.getContext("2d").getImageData(sx, sy, w, h).data;
pixels = [];
for (y = _i = sy, _ref = sy + h; _i < _ref; y = _i += 1) {
indexBase = y * w * 4;
for (x = _j = sx, _ref1 = sx + w; _j < _ref1; x = _j += 1) {
index = indexBase + (x * 4);
pixels.push([pdata[index], pdata[index + 1], pdata[index + 2]]);
}
}
return (new MMCQ).quantize(pixels, nc);
};

但是,等等,我们没有<canvas/> TVML 中的元素!

当然也有像Objective-C这样的原生解决方案ColorCube , DominantColor - 这是使用 K-means

和非常漂亮且可重复使用的 ColorArt来自 CocoaControls 的@AaronBrethorst。

尽管这可以通过原生到 JavaScriptCore 的桥接在 TVML 应用程序中使用 - 请参阅 How to bridge TVML/JavaScriptCore to UIKit/Objective-C (Swift)?

我的目标是在 TVJS 中完成这项工作和 TVML .

最简单的 MMCQ JavaScript 实现不需要 Canvas:参见 Basic Javascript port of the MMCQ (modified median cut quantization)通过 Nick Rabinowitz ,但需要图像的 RGB 数组:

var cmap = MMCQ.quantize(pixelArray, colorCount);

取自 HTML <canvas/>这就是原因!

function createPalette(sourceImage, colorCount) {

// Create custom CanvasImage object
var image = new CanvasImage(sourceImage),
imageData = image.getImageData(),
pixels = imageData.data,
pixelCount = image.getPixelCount();

// Store the RGB values in an array format suitable for quantize function
var pixelArray = [];
for (var i = 0, offset, r, g, b, a; i < pixelCount; i++) {
offset = i * 4;
r = pixels[offset + 0];
g = pixels[offset + 1];
b = pixels[offset + 2];
a = pixels[offset + 3];
// If pixel is mostly opaque and not white
if (a >= 125) {
if (!(r > 250 && g > 250 && b > 250)) {
pixelArray.push([r, g, b]);
}
}
}

// Send array to quantize function which clusters values
// using median cut algorithm

var cmap = MMCQ.quantize(pixelArray, colorCount);
var palette = cmap.palette();

// Clean up
image.removeCanvas();

return palette;
}

[问题]如何在不使用 HTML5 的情况下生成 RGB 图像的主色 <canvas/> , 但在来自图像的 ByteArray 的纯 JavaScript 中用 XMLHttpRequest 获取?

[更新]我已将此问题发布到 Color-Thief github repo,使 RGB 数组计算适应最新的代码库。我试过的解决方案是这个

ColorThief.prototype.getPaletteNoCanvas = function(sourceImageURL, colorCount, quality, done) {
var xhr = new XMLHttpRequest();
xhr.open('GET', sourceImageURL, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {

var uInt8Array = new Uint8Array(this.response);
var i = uInt8Array.length;
var biStr = new Array(i);
while (i--)
{ biStr[i] = String.fromCharCode(uInt8Array[i]);
}

if (typeof colorCount === 'undefined') {
colorCount = 10;
}
if (typeof quality === 'undefined' || quality < 1) {
quality = 10;
}

var pixels = uInt8Array;
var pixelCount = 152 * 152 * 4 // this should be width*height*4

// Store the RGB values in an array format suitable for quantize function
var pixelArray = [];
for (var i = 0, offset, r, g, b, a; i < pixelCount; i = i + quality) {
offset = i * 4;
r = pixels[offset + 0];
g = pixels[offset + 1];
b = pixels[offset + 2];
a = pixels[offset + 3];
// If pixel is mostly opaque and not white
if (a >= 125) {
if (!(r > 250 && g > 250 && b > 250)) {
pixelArray.push([r, g, b]);
}
}
}

// Send array to quantize function which clusters values
// using median cut algorithm
var cmap = MMCQ.quantize(pixelArray, colorCount);
var palette = cmap? cmap.palette() : null;
done.apply(this,[ palette ])

} // 200
};
xhr.send();
}

但它不会返回正确的 RGB 颜色数组。

[更新]感谢所有的建议,我让它工作了。现在可以在 Github 上找到完整示例,

最佳答案

canvas 元素被用作将图像解码为 RGBA 数组的便捷方式。您还可以使用纯 JavaScript 库来进行图像解码。

jpgjs是一个 JPEG 解码器和 pngjs是PNG解码器。看起来 JPEG 解码器将按原样与 TVJS 一起工作。然而,PNG 解码器看起来像是为在 Node 或 Web 浏览器环境中工作而设计的,因此您可能需要稍微调整一下。

关于javascript - 使用 XMLHttpRequest 为 RGB 图像生成主色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33312362/

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