# Color image to black and white image

**URL:** https://discourse.processing.org/t/color-image-to-black-and-white-image/8662
**Category:** Libraries
**Created:** [February 23, 2019, 1:05pm UTC](https://discourse.processing.org/t/color-image-to-black-and-white-image/8662 "2019-02-23T13:05:06Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Grisha](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@Grisha](https://discourse.processing.org/u/Grisha)
#### Post date: [February 23, 2019, 1:05pm UTC](https://discourse.processing.org/t/color-image-to-black-and-white-image/8662/1 "2019-02-23T13:05:06Z")

</div>

How to make a black and white image from color image . Maybe using OpenCV? Because I don’t need combination of black and white pixels , but I need a gray gradient image ( I hope you understand what I mean )

---

<div class="post-metadata">

### Author: ![figraham](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/figraham/32/4161_2.png) [@figraham](https://discourse.processing.org/u/figraham)
#### Post date: [February 23, 2019, 1:52pm UTC](https://discourse.processing.org/t/color-image-to-black-and-white-image/8662/2 "2019-02-23T13:52:07Z")

</div>

There’s a number of ways to do this. The easiest way is to use the [`filter()` function](https://processing.org/reference/filter_.html). The filter function can be used on the entire sketch or you can use it on a [PImage](https://processing.org/reference/PImage_filter_.html). You could also do it manually by looping through the pixels of an image like:

```auto
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++) {
  img.pixels[i] = color(brightness(img.pixels[i]));
}
img.updatePixels();

```

Which would also give you more control if you want to do something other than the basic filters.

It sounds like you’re looking for a grayscale image but if you want one thats just black and white pixels. You can use [dithering](https://en.wikipedia.org/wiki/Dither) and [here’s a video about dithering in processing](https://www.youtube.com/watch?v=0L2n8Tg2FwI).

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 24, 2019, 7:07pm UTC](https://discourse.processing.org/t/color-image-to-black-and-white-image/8662/3 "2019-02-24T19:07:32Z")

</div>

As figraham says, `filter(GRAY)` is probably what you want.

If you are looking to design your own custom black and white (or gray) effect, then in addition to dithering directly in Processing as in the video you can also check out PShader. The PShader includes an example of a black and white shader:

> Code listing 8.1: Sketch using the black&white shader.  
> [https://processing.org/tutorials/pshader/](https://processing.org/tutorials/pshader/)

![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9d1a96cd55fee4a16dcf042d638b9f35f90d6c1b.png)

and there are lots of other photo filter effects that you can do using shaders. These tend to be advanced projects, however, as you are working in a different language (GLSL) to write them.
