简体   繁体   English

如何使图像黑白?

[英]How to make the Image black and white?

I have created Image. 我已经创建了图像。 How to convert color Image to black&white. 如何将彩色图像转换为黑白图像。 For example, on mouse over it. 例如,将鼠标悬停在其上。

First open it in a decent image editor, convert it to grayscale (I assume that you want more colors than black and white ;) ), save it in the webapplication. 首先在合适的图像编辑器中将其打开,将其转换为灰度 (我假设您想要的颜色多于黑白;)),将其保存在Web应用程序中。

Then, on the mouseover just call some JavaScript function which changes the src of the HTML <img> element to point to the URL of the grayscale image instead. 然后,在鼠标悬停时,只需调用一些JavaScript函数即可,该函数将HTML <img>元素的src更改为指向灰度图像的URL。

Basic kickoff example: 基本的启动示例:

<img src="color.jpg" onmouseover="this.src='blackwhite.jpg'">

The easiest way is to create another image in black and white server side (or using an image editing program if the image isn't dynamic) and swap out the color one for b&w on mouse over. 最简单的方法是在黑白服务器端创建另一张图像(如果图像不是动态图像,则使用图像编辑程序),然后在鼠标悬停时将颜色换成黑白。

edit 编辑

Then just just set the opacity using css: http://www.quirksmode.org/css/opacity.html 然后只需使用CSS设置不透明度即可: http : //www.quirksmode.org/css/opacity.html

The Pixastic JS Image Manipulation library has this feature, which you can call via JSNI. Pixastic JS图像处理库具有此功能,您可以通过JSNI进行调用。

There's also this script which appears to only work in IE using proprietary Microsoft magic. 还有这个脚本似乎只能在使用专有Microsoft Magic的IE中工作。

But honestly, just generate a desaturated image for every image you want on the server-side, and swap them out using JS/CSS. 但老实说,只要在服务器端为每个想要的图像生成一个去饱和的图像,然后使用JS / CSS换出它们即可。

img {
  transition: filter .5s ease-in-out;
  -webkit-filter: grayscale(0%); /* Ch 23+, Saf 6.0+, BB 10.0+ */
  filter: grayscale(0%); /* FF 35+ */
}
img:hover {
  -webkit-filter: grayscale(100%); /* Ch 23+, Saf 6.0+, BB 10.0+ */
  filter: grayscale(100%); /* FF 35+ */
}

The Black and White effect is achieved with the grayscale value for the filter property. 使用滤镜属性的灰度值可实现黑白效果。 grayscale(100%) corresponds to Black and White and grayscale(0%) corresponds to full colors. 灰度(100%)对应于黑白,灰度(0%)对应于全彩色。 The -webkit-filter prefix is necessary for that property The smooth animation is achieved with the transition property 该属性必须使用-webkit-filter前缀。使用transition属性可以实现流畅的动画

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM