当前位置:网站首页>Use a grayscale filter
Use a grayscale filter
2022-07-27 23:50:00 【Never, calm】
Turn the picture gray
<script>
function loadImage(src) {
const img = new Image();
img.crossOrigin = 'anonymous';
return new Promise((resolve) => {
img.onload = () => {
resolve(img);
};
img.src = src;
});
}
const imageDataContext = new WeakMap();
// Get pictures of imageData data
function getImageData(img, rect = [0, 0, img.width, img.height]) {
let context;
if(imageDataContext.has(img)) context = imageDataContext.get(img);
else {
const canvas = new OffscreenCanvas(img.width, img.height);
context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
imageDataContext.set(img, context);
}
return context.getImageData(...rect);
}
// Loop traversal imageData data
function traverse(imageData, pass) {
const {
width, height, data} = imageData;
for(let i = 0; i < width * height * 4; i += 4) {
const [r, g, b, a] = pass({
r: data[i] / 255,
g: data[i + 1] / 255,
b: data[i + 2] / 255,
a: data[i + 3] / 255,
index: i,
width,
height,
x: ((i / 4) % width) / width,
y: Math.floor(i / 4 / width) / height});
data.set([r, g, b, a].map(v => Math.round(v * 255)), i);
}
return imageData;
}
const canvas = document.getElementById('paper');
const context = canvas.getContext('2d');
(async function () {
// Loading images asynchronously
const img = await loadImage('https://p2.ssl.qhimg.com/d/inn/4b7e384c55dc/girl1.jpg');
// Get the imageData Data objects
const imageData = getImageData(img);
// Traverse imageData Data objects
traverse(imageData, ({
r, g, b, a}) => {
// Grayscale each pixel
const v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return [v, v, v, a];
});
// to update canvas Content
canvas.width = imageData.width;
canvas.height = imageData.height;
context.putImageData(imageData, 0, 0);
}());
</script>
边栏推荐
- Record the errors about formatc in R language
- BUUCTF-RSA
- MySQL data query (where)
- Reinforcement learning - pytorch realizes advantage actor critical (A2C)
- Which one is better to request to merge -- three skills of interface request merging, and the performance directly explodes the table
- 【JS 逆向百例】某公共资源交易网,公告 URL 参数逆向分析
- C#委托用法--控制台项目,通过委托实现事件
- Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
- proteus仿真arduino中调用DHT11/22温湿度传感器
- Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
猜你喜欢
随机推荐
Control mode of CPU
Zabbix4.0 uses SNMP agent to monitor vcenter6.5
Solve 5g pain points, Meizu 17 smart 5g fast and stable technology release
In 2019, the world's top ten semiconductor manufacturers: Intel returned to the first place, and apple rose sharply against the trend
BUUCTF-Dangerous RSA
实现按照序号命名的txt文件由后往前递补重命名文件
Design and implementation of spark offline development framework
重新定义分析 - EventBridge 实时事件分析平台发布
BUUCTF-childRSA费马小定理
UE4官方AEC蓝图案例课程学习笔记
携手长江存储,江波龙推出全球最小扩展卡
Nail alarm tool
Explain the idempotence of distributed system in detail
Is it really hard to understand? What level of cache is the recyclerview caching mechanism?
Redis hash underlying data structure
TCP的粘包拆包问题+解决方案
Reinforcement learning - pytorch realizes advantage actor critical (A2C)
MySQL data query (where)
[NPUCTF2020]EzRSA
Unity 实现简单画板画画功能(笔记)









