<template> <div class="container"> <img :src="originalImage" class="original" @load="convertToBlackAndWhite"> <img :src="blackAndWhiteImage" class="black-and-white" v-show="blackAndWhite"> </div> </template> <script> export default { data() { return { originalImage: 'images/pic.jpg', blackAndWhiteImage: '', blackAndWhite: false }; }, methods: { convertToBlackAndWhite() { let canvas = document.createElement('canvas'); let ctx = canvas.getContext('2d'); let img = document.querySelector('.original'); canvas.width = img.width; canvas.height = img.height; ctx.drawImage(img, 0, 0); let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); let data = imageData.data; for (let i = 0; i < data.length; i += 4) { let brightness = (data[i] + data[i + 1] + data[i + 2]) / 3; if (brightness > 128) { data[i] = 255; data[i + 1] = 255; data[i + 2] = 255; } else { data[i] = 0; data[i + 1] = 0; data[i + 2] = 0; } } ctx.putImageData(imageData, 0, 0); this.blackAndWhiteImage = canvas.toDataURL(); this.blackAndWhite = true; } } }; </script> <style scoped> .container { display: flex; justify-content: center; align-items: center; height: 100vh; } .original, .black-and-white { max-width: 300px; } .black-and-white { display: none; } </style>
标签: Vue图片处理
上面是“如何在Vue中实现图片的黑白处理?”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_12896.html
workflows工作流
- 一只透明老虎骨骼标本ComfyUI工作流
- 一尊白玉佛像ComfyUI工作流
- 一只由粉色水晶与羽毛组成的巨型高跟鞋
- 1个可爱的白色短发女孩
- 一个20岁丰满的女孩ComfyUI工作流
- 海中一头鲸鱼ComfyUI工作流
- 一个女孩骑着一辆生锈的现代摩托车
- 一个男人正走进科幻的大门ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!