# Reading pixels from an image

**URL:** <https://discourse.processing.org/t/reading-pixels-from-an-image/31592>\
**Category:** Coding Questions\
**Created:** [August 6, 2021, 7:48am UTC](https://discourse.processing.org/t/reading-pixels-from-an-image/31592 "2021-08-06T07:48:57Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [August 6, 2021, 11:01am UTC](https://discourse.processing.org/t/reading-pixels-from-an-image/31592/10 "2021-08-06T11:01:06Z")

</div>

OK, let’s try another piece of code with the `polar_bear.jpg` file:

```auto
let img;

function preload() {
  img = loadImage("polar_bear.jpg");
}

function setup() {
  createCanvas(653, 320);
  noLoop();
}

function draw() {
  img.loadPixels();
  for (let i = 0; i < img.pixels.length; i += 4) {
    img.pixels[i + 1] = 0;
    img.pixels[i + 2] = 0;
  }
  img.updatePixels();
  image(img, 0, 0);
}

```

If you run it exactly as is, you should see the image with only its red component. Please let us know what happens.

It is best to format code when you post it. See [Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147). It includes information on formatting code.

---

_[View the full topic](https://discourse.processing.org/t/reading-pixels-from-an-image/31592)._
