- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 JavaScript 中的 Canvas 转换为 pgm。在我转换文件并将其下载到我的 table 面后,它具有 pgm 扩展名,但是当我用编辑器打开它时,它被编码为 png。我认为如果 JavaScript 无法识别您要转换为的扩展名,它会默认将其转换为 png。所以 JavaScript 可以转换为 png、jpeg 等,但不能转换为 pgm?
我的问题是是否可以使用 JavaScript 将 canvas 转换为 pgm,如果没有,是否有任何可用的插件或 Js 库可以这样做。
function image_download() {
canvas = $('#canvas').get(0);
var image = new Image();
image.src = canvas.toDataURL("image/pgm");
var download = document.createElement('a');
download.href = image.src;
download.download = 'map.pgm';
download.click();
}
最佳答案
为了将 Canvas 保存为 PGM,我刚去了 http://netpbm.sourceforge.net/doc/pgm.html其中详细说明了文件格式,并使用该信息创建了一个将 Canvas 转换为 PGM bin 的函数。
文件有一个描述文件和内容的头,然后是一个二进制值数组,8 位或 16 位
所以从标题开始并将其创建为字符串
const CR = "\n";
var header = "P5" // required
header += CR; // add whitespace char
header += canvas.width;
header += CR; // add whitespace char
header += canvas.height;
header += CR; // add whitespace char
header 不完整,但我们需要获取像素值的最大值,以便转换图像并找到最大值。
因此通过 2D 上下文获取像素数据。
var imgData = ctx.getImageData(0,0,ctx.canvas.width,ctx.height);
关于您想要的数据,您有多种选择。该格式为某些值指定对数标度,为其他值指定线性标度。如果保持 Gamma 很重要,我会留给你找到转换。 (请注意,仅当您计划广播或保存光值(例如天文图像)时才需要)
有一个实用程序可以将线性 Gamma 函数转换为 BT.709 Gamma 函数。查看链接文档
我添加了一些简单的格式。两个为16位,最大值为3*255。
如果您从 jpeg 编码图像转换,最好通过首先将像素 RGB 转换为 HSL 并仅将 L 保存为 8 位值来从图像中提取亮度,这将阻止 jpeg 质量差的色调和饱和度数据影响结果(jpeg 具有近乎无损的灰度数据)
const formats = {
mean : {
convert (r, g, b) { bin[j++] = ((r + g + b) / 3 ) | 0 },
dataStore (w, h) { return new Uint8Array(w * h) },
},
summed : {
convert (r, g, b) { bin[j++] = r + g + b },
dataStore (w, h) { return new Uint16Array(w * h) },
},
meanLog : {
convert (r, g, b) { bin[j++] = Math.sqrt((r * r + g * g + b * b)/ 3) | 0 },
dataStore (w, h) { return new Uint8Array(w * h) },
},
summedLog : {
convert (r, g, b) { bin[j++] = Math.sqrt(r * r + g * g + b * b) | 0 },
dataStore (w, h) { return new Uint16Array(w * h) },
}
}
var imgData = ctx.getImageData(0,0,ctx.canvas.width,ctx.canvas.height);
var format = formats.mean;
var bin = format.dataStore(canvas.width, canvas.height);
var r,g,b,summed,mean,summedLog,meanLog;
var i = 0;
var j = 0;
var max = 0;
var d = imgData.data; // shorthand var to pixel data
while(i < d.length){
format.convert(d[i++],d[i++],d[i++]);
max = Math.max(bin[j-1],max);
i++; // skip alpha
}
header += max; // max value.
header += CR; // add whitespace char
// We have all that is needed to create the file so first convert
// header string to ascII
var fileBin = new Uint8Array(header.length + bin.length);
for(i = 0; i < header.length; i++){
fileBin[i] = header.charCodeAt(i) & 0b01111111; // strip of top bit just in case superstitious coder reads this
}
fileBin.set(new Uint8Array(bin.buffer),header.length); // add the pixel data
// fileBin is the 8bit bin of the PGM file
// Note transparent pixels are black.
// Note I do not check for all black. Format doc indicates that a max val in the head of zero is illegal. What that means you will have to find out.
就是这样。您可以将其转换为 Base64 编码,或者按原样保存
请注意,在此答案之前我从未使用过或听说过这种图像格式,上面的代码是对答案顶部链接的文档的快速解释。如果它有效,我不知道,因为我没有任何东西可以测试它。该代码没有语法和运行时错误。
它更像是展示如何为特定格式创建二进制文件的指南
我想我还会添加一种格式来处理 JPEG 图像,它只需要一个良好的 RGB 到 HSL 函数。
// NOTE to prevent data loss the out put of lum has been
// scaled from the normal 0-100 to 0-255 for the example PGM image format
// If you use this function to get HSL values compatible with HSL CSS
// colours change the multipliers from 255 to 100. Commented with [*1]
function rgb2hsl(r,g,b){ // integers in the range 0-255
var minC, maxC, dif, h, l, s;
h = l = s = 0;
r /= 255; // normalize channels
g /= 255;
b /= 255;
min = Math.min(r, g, b);
max = Math.max(r, g, b);
if(min === max){ // [*1] no colour so early exit
return {
h, s,
l : Math.floor(min * 255), // this normally is 0-100
}
}
dif = max - min;
l = (max + min) / 2;
if (l > 0.5) { s = dif / (2 - max - min) }
else { s = dif / (max + min) }
if (max === r) {
if (g < b) { h = (g - b) / dif + 6.0 }
else { h = (g - b) / dif }
} else if(max === g) { h = (b - r) / dif + 2.0 }
else {h = (r - g) / dif + 4.0 }
h = Math.floor(h * 60);
s = Math.floor(s * 100);
l = Math.floor(l * 255); // [*1] this normally is 0-100
return {h, s, l};
},
然后将以下内容添加到格式对象
formats.jpegLumPreserve = {
convert (r, g, b) { bin[j++] = rgb2hsl(r + g + b).l},
dataStore (w, h) { return new Uint8Array(w * h) },
}
如果您从最初存储为 jpeg (jpg) 的图像中保存,您应该使用此格式
format = formats.jpegLumPreserve;
因为总是有一些聪明人啊...需要兜售感知亮度转换的人,我也会补充一点,因为他们通常会弄错。正如我认为转换是一个老笑话,转换基于 2/7/1 规则并在对数空间中完成。
formats.perceptual = {
convert (r, g, b) { bin[j++] = Math.sqrt(r * r * 0.2 + g * g * 0.7 + b * b * 0.1) | 0},
dataStore (w, h) { return new Uint8Array(w * h) },
}
这是基于成年人感知红色、绿色和蓝色 3 种原色并将灰色(亮度)结果与感知颜色亮度相匹配的平均能力。
关于JavaScript 将 Canvas 转换为 PGM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260008/
我有一个 html 格式的表单: 我需要得到 JavaScript在value input 字段执行,但只能通过表单的 submit .原因是页面是一个模板所以我不控制它(不能有
我管理的论坛是托管软件,因此我无法访问源代码,我只能向页面添加 JavaScript 来实现我需要完成的任务。 我正在尝试用超链接替换所有页面上某些文本关键字的第一个实例。我还根据国家/地区代码对这些
我正在使用 JS 打开新页面并将 HTML 代码写入其中,但是当我尝试使用 document.write() 在新页面中编写 JS 时功能不起作用。显然,一旦看到 ,主 JS 就会关闭。用于即将打开的
提问不是为了解决问题,提问是为了更好地理解系统 专家!我知道每当你将 javascript 代码输入 javascript 引擎时,它会立即由 javascript 引擎执行。由于没有看过Engi
我在一个文件夹中有两个 javascript 文件。我想将一个变量的 javascript 文件传递到另一个。我应该使用什么程序? 最佳答案 window.postMessage用于跨文档消息。使
我有一个练习,我需要输入两个输入并检查它们是否都等于一个。 如果是 console.log 正则 console.log false 我试过这样的事情: function isPositive(fir
我正在做一个Web应用程序,计划允许其他网站(客户端)在其页面上嵌入以下javascript: 我的网络应用程序位于 http://example.org 。 我不能假设客户端网站的页面有 JQue
目前我正在使用三个外部 JS 文件。 我喜欢将所有三个 JS 文件合而为一。 尽一切可能。我创建 aio.js 并在 aio.js 中 src="https://code.jquery.com/
我有例如像这样的数组: var myArray = []; var item1 = { start: '08:00', end: '09:30' } var item2 = {
所以我正在制作一个 Chrome 扩展,它使用我制作的一些 TamperMonkey 脚本。我想要一个“主”javascript 文件,您可以在其中包含并执行其他脚本。我很擅长使用以下行将其他 jav
我有 A、B html 和 A、B javascript 文件。 并且,如何将 A JavaScript 中使用的全局变量直接移动到 B JavaScript 中? 示例 JavaScript) va
我需要将以下整个代码放入名为 activate.js 的 JavaScript 中。你能告诉我怎么做吗? var int = new int({ seconds: 30, mark
我已经为我的 .net Web 应用程序创建了母版页 EXAMPLE1.Master。他们的 I 将值存储在 JavaScript 变量中。我想在另一个 JS 文件中检索该变量。 示例1.大师:-
是否有任何库可以用来转换这样的代码: function () { var a = 1; } 像这样的代码: function () { var a = 1; } 在我的浏览器中。因为我在 Gi
我收到语法缺失 ) 错误 $(document).ready(function changeText() { var p = document.getElementById('bidp
我正在制作进度条。它有一个标签。我想调整某个脚本完成的标签。在找到可能的解决方案的一些答案后,我想出了以下脚本。第一个启动并按预期工作。然而,第二个却没有。它出什么问题了?代码如下: HTML:
这里有一个很简单的问题,我简单的头脑无法回答:为什么我在外部库中加载时,下面的匿名和onload函数没有运行?我错过了一些非常非常基本的东西。 Library.js 只有一行:console.log(
我知道 javascript 是一种客户端语言,但如果实际代码中嵌入的 javascript 代码以某种方式与在控制台上运行的代码不同,我会尝试找到答案。让我用一个例子来解释它: 我想创建一个像 Mi
我如何将这个内联 javascript 更改为 Unobtrusive JavaScript? 谢谢! 感谢您的回答,但它不起作用。我的代码是: PHP js文件 document.getElem
我正在寻找将简单的 JavaScript 对象“转储”到动态生成的 JavaScript 源代码中的最优雅的方法。 目的:假设我们有 node.js 服务器生成 HTML。我们在服务器端有一个对象x。
我是一名优秀的程序员,十分优秀!