css如何把图片设置为黑白

Desaturating a color image couldn’t be simpler with CSS. The filter is typically applied as a class, as you will often want several images to have the same visual effect:

用CSS对彩色图像进行饱和绝非易事 。 滤镜通常作为class来应用,因为您通常希望多个图像具有相同的视觉效果:

img.desaturate {filter: grayscale(100%);
}

(Note the spelling of “grayscale”; the alternative spelling will not work)

(请注意“灰度”的拼写;其他拼写将不起作用)

Applying the class to the image is also straightforward:

将类应用于图像也很简单:

<img src="lena-söderberg.jpg" alt="Lena Söderberg" class="desaturate">

添加SVG滤镜效果 (Add An SVG Filter Effect)

The CSS shown to this point works in all modern browsers on desktop and mobile, including Microsoft Edge.

至此所示CSS在台式机和移动设备上的所有现代浏览器(包括Microsoft Edge)中均可使用。

To gain the same effect in Firefox previous to version 35, we need to use an SVG filter, which I’ll create as a separate document named desaturate.svg. The code for that file will be:

为了在版本35之前的 Firefox中获得相同的效果,我们需要使用SVG过滤器,我将其创建为名为desaturate.svg的单独文档。 该文件的代码将是:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"><filter id="greyscale"><feColorMatrix type="matrix" values="0.3333 0.3333 0.3333 0 00.3333 0.3333 0.3333 0 00.3333 0.3333 0.3333 0 00 0 0 1 0" /></filter>
</svg>

If the SVG code looks slightly daunting – and the matrix math behind it is somewhat complex – don’t worry. This is one piece of code that I’d actually encourage you to copy and paste as a generic “recipe”; I’ll explain matrix transformations in a future article.

如果SVG代码看起来略微令人望而生畏-和它背后的矩阵数学有些复杂-不要担心。 这是我实际上鼓励您复制并粘贴为通用“食谱”的一段代码; 我将在以后的文章中解释矩阵转换。

With the SVG file saved beside our HTML page and test image, the CSS is extended to become:

通过将SVG文件保存在我们的HTML页面和测试图像旁边,CSS扩展为:

img.desaturate {filter: grayscale(100%);filter: url(desaturate.svg#greyscale);
}

添加对IE的支持 (Add Support for IE)

To cover IE 6 – 9, we'll apply Microsoft’s simple but proprietary use of filter:

为了涵盖IE 6 – 9,我们将应用Microsoft简单但专有的filter用法:

img.desaturate{filter: gray;filter: grayscale(100%);filter: url(desaturate.svg#greyscale);
}

If you want to add in support for still older versions of Webkit:

如果要添加对更旧版本的Webkit的支持:

img.desaturate{-webkit-filter: grayscale(1);-webkit-filter: grayscale(100%);filter: gray;filter: grayscale(100%);filter: url(desaturate.svg#greyscale);
}

As noted in the Cross Browser Image Blur Effects article, this technique won't currently work in Internet Explorer 10 or 11. If you wanted to achieve the same visual result across absolutely every browser you could use a cross browser JavaScript solution. Alternatives include Greyscale.js.

正如“ 跨浏览器图像模糊效果”一文中指出的那样,该技术目前在Internet Explorer 10或11中不起作用。如果您想在绝对所有浏览器上都获得相同的视觉效果,则可以使用跨浏览器JavaScript解决方案 。 替代方法包括Greyscale.js 。

The CSS we've written here allows us to visually convert an image to black and white on the fly in our browser, with no need to save new versions in PhotoShop. Using CSS also makes the image much easier to modify: for example, you’ll see that lowering the percentage used in our declaration from 100% to 50% causes a visual blend of the desaturation effect with the original color image.

我们在这里编写CSS使我们能够在浏览器中将图像实时地转换为黑白图像,而无需在PhotoShop中保存新版本。 使用CSS还可以使图像更容易修改:例如,您会看到将声明中使用的百分比从100%降低到50%会导致去饱和效果与原始彩色图像的视觉混合。

A slightly easier approach for older versions of Firefox inlines the SVG into the CSS directly, removing the need for any SVG code in the <body>:

对于较旧版本的Firefox而言,一种更简单的方法是将SVG直接内联到CSS中,从而无需<body>中的任何SVG代码:

img.desaturate {-webkit-filter: grayscale(100%);filter: grayscale(100%);filter: gray;filter: url("data:image/svg+xml;utf8,<svg version='1.1' xmlns='http://www.w3.org/2000/svg' height='0'><filter id='greyscale'><feColorMatrix type='matrix' values='0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0' /></filter></svg>#greyscale");
}

翻译自: https://thenewcode.com/532/Convert-Images-To-Black-And-White-With-CSS

css如何把图片设置为黑白

css如何把图片设置为黑白_使用CSS将图像转换为黑白图像相关推荐

  1. [css] 给一个图片设置透明有哪些方式呢?

    [css] 给一个图片设置透明有哪些方式呢? 1.opacity : 0 -> 子元素会继承 2.外层用盒子包裹,设置其rgba(255,255,255,0) 个人简介 我是歌谣,欢迎和大家一起 ...

  2. CSS 网页背景图片设置

    <!DOCTYPE html> <html> <head><meta charset="utf-8"><style>#l ...

  3. css设置按钮样式_使用CSS设置按钮样式的快速指南

    css设置按钮样式 by Ashwini Sheshagiri 通过Ashwini Sheshagiri Buttons have become an inevitable part of front ...

  4. gradient设置上下渐变_图解CSS: CSS渐变

    CSS的渐变主要分布在 conic-gradient()和repeating-conic-gradient()两个属性.在CSS中,CSS的渐变相当于图像,我们可以使用线性渐变(linear-grad ...

  5. android将彩图转为黑白_如何快速修出高质感黑白照片

    作者:曼哈顿印象 微博:曼哈顿印象PHOTO 视频教程 视频教程​www.bilibili.com 哈喽,大家好,我是小曼(曼哈顿印象),今天给大家带来一期照片转黑白的教程,利用PS快速修出高质量的黑 ...

  6. java 图片 黑白_java – 将图像转换为黑白(不是灰色标尺)

    你好我将图像从彩色转换为纯黑色和白色,结果是一个黑暗的图像.我没理由.以下是我的代码,它受到SO上其他代码的启发. 任何指导都会有所帮助. BufferedImage coloredImage = I ...

  7. css怎样将图片设置成正方形,而且随着浏览器窗口大小的改变而自适应缩放

    效果图如下: 代码实例如下: <!doctype html> <html lang="en"><head><meta charset=&q ...

  8. css:css样式背景图片设置透明度,css如何设置背景图片的透明度

    1. 了解css如何设置背景图片的透明度 注意: css中无法直接给背景图片加 opacity 属性,可以使用下面的方法来给background-image加上我们想要的透明度opacity. 2. ...

  9. hssfworkbook 设置自适应宽度_「CSS很简单」CSS 实现宽高等比自适应容器

    在最近开发移动端页面,遇到这么一个情况:当页面宽度 100% 时,高度为宽度一半,并随手机宽度变化依然是一半. 于是我们就需要实现一个宽度自适应,高度为宽度一半的容器. 这里先以高度为宽度一半为例,也 ...

最新文章

  1. 1. 青蛙跳跳FrogJmp Count minimal number of jumps from position X to Y.
  2. MySQL关闭查询缓存(QC)的两种方法
  3. 国产操作系统要起来,这款Linux是你的菜吗?
  4. iOS之AVPlayerViewController的使用oc
  5. Android之在笔记本电脑adb devices识别不了oppo A9手机(设备管理器ADB Interface里面有个黄色感叹号)
  6. java提高篇之抽象类与接口
  7. 动画讲解C语言的指针,从未如此简单
  8. LeetCode 1553. 吃掉 N 个橘子的最少天数(BFS)
  9. mysq命令行导出sql_mysql 命令行导入导出 sql
  10. java file 字符串_Java读取一个文本文件拼接成一个字符串(readFileToString)
  11. 背包问题 codevs2210 数字组合
  12. 《无边界触达——数字化时代的高等教育》白皮书,附下载地址
  13. 超实用的JavaScript技巧及最佳实践
  14. ftp服务器生成文件,ftp服务器自动生成文件
  15. 百度网盘欲分拆闯关科创板?
  16. 个推如何管理亿级代码的质量?持续集成SonarQube 代码质量管理系统
  17. Java 实现视频下载功能
  18. python清洗数据去除停用词_python去除停用词(结巴分词下)
  19. RS485串口Modbus设备联网解决方案
  20. c语言 源文件未编译,源文件未编译什么意思

热门文章

  1. python爬虫中requests库和正则表达式之淘宝爬虫实战
  2. 华为p50预计售价鸿蒙是什么,华为P50没赶上首发鸿蒙系统,还有哪些值得期待的地方?...
  3. ValueAnimator 中文翻译
  4. 暗灰色android代码,Android实现制作灰色图片
  5. vidio视频播放,拿来即用,转换手机横屏问题
  6. 轻轻松松做网吧桌面同步
  7. springboot调用ffmpeg完成有音频的视频与背景音乐合并
  8. JS面试(四):对调变量的六种方法
  9. 世界环保创业基金会简介
  10. 【Python 星球1.0】 Python简介