当前位置:网站首页>使用灰度滤镜
使用灰度滤镜
2022-07-27 20:36:00 【从未、淡定】
将图片变成灰色
<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();
// 获得图片的 imageData 数据
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);
}
// 循环遍历 imageData 数据
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 () {
// 异步加载图片
const img = await loadImage('https://p2.ssl.qhimg.com/d/inn/4b7e384c55dc/girl1.jpg');
// 获取图片的 imageData 数据对象
const imageData = getImageData(img);
// 遍历 imageData 数据对象
traverse(imageData, ({
r, g, b, a}) => {
// 对每个像素进行灰度化处理
const v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return [v, v, v, a];
});
// 更新canvas内容
canvas.width = imageData.width;
canvas.height = imageData.height;
context.putImageData(imageData, 0, 0);
}());
</script>
边栏推荐
- 八大排序之冒泡、快排、堆排、基数排序
- Promise solves asynchrony
- Exercise --- BFS
- 用户画像在科技期刊微信公众号精准推送中的应用
- [soft test software evaluator] 2014 comprehensive knowledge over the years
- [GNN report] Tang Jian, Montreal, Canada: Geometric deep learning for drug discovery
- Parameter transmission of components
- Main security risks and Countermeasures of cloud computing services
- 只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
- Security-001
猜你喜欢

干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)

Pro multi store version system, versatile is it!

Pyqt5 rapid development and practice 4.9 dialog controls

Find and leverage xxE – XML external entity injection

Object creation process and object layout

WWW 2019 | HAN:异质图注意力网络

习题 --- BFS

Process and planned task management

The principle and demonstration of service path lifting without quotation marks

简单实用的数据可视化案例
随机推荐
Bi Shi - University Logistics Management System Based on SSM
Blood spitting finishing nanny level series tutorial - playing Fiddler bag capturing tutorial (5) - detailed explanation of fiddler monitoring panel
华为鸿蒙 3 正式发布,这个安全功能解决了一大痛点
记录一下使用R语言中关于formatC的错误
51 MCU internal peripherals: real time clock (SPI)
Find and leverage xxE – XML external entity injection
图基本知识代码
Vulnhub range double trouble
kubevela通过CLI部署应用
MySQL basic installation and startup
Www 2019 | Han: heterograph attention network
On data management of data warehouse
Dry goods semantic web, Web3.0, Web3, metauniverse, these concepts are still confused? (medium)
【GNN报告】加拿大蒙特利尔唐建:Geometric Deep Learning For Drug Discovery
Cloudcompare & PCL point cloud equally spaced slices
Read an article to understand artificial neural network
常用泰勒展开
Library management system based on SSM framework
How to quickly view the API properties and usage of the h.265 video player easyplayer?
Shuffle, partition and read of tfrecord