# Write color pixel to an Image

**URL:** https://discourse.processing.org/t/write-color-pixel-to-an-image/42849
**Category:** Coding Questions
**Created:** [September 28, 2023, 2:30pm UTC](https://discourse.processing.org/t/write-color-pixel-to-an-image/42849 "2023-09-28T14:30:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![matheplica](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/matheplica/32/21035_2.png) [@matheplica](https://discourse.processing.org/u/matheplica)
#### Post date: [September 28, 2023, 2:30pm UTC](https://discourse.processing.org/t/write-color-pixel-to-an-image/42849/1 "2023-09-28T14:30:13Z")

</div>

Hi, I’m trying to draw green pixel from a layer to a PImage.  
But I’ve got this result unexpected :  
 ![pixelbug](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/4/c/4c7cfc52ca480e7d28a00c4804e80382bc2343ae.jpeg)  
The code seems ok, there is something I don’t understand.

```auto
PImage result;
PGraphics msk;
void setup() {
  size(360, 200);
  msk = createGraphics(width, height);
  result = createImage(width, height, ARGB);
  noLoop();
}
void draw() {
  msk.beginDraw();
  msk.background(0, 255);
  msk.stroke(0, 255, 0);
  msk.noFill();
  msk.strokeWeight(6);
  msk.circle(260, 60, 32);
  msk.loadPixels();
  for (int i=0; i<width; i++) {
    for (int j=0; j<height; j++) {
      if (msk.pixels[i*height+j]==#FF00FF00) {
        result.set(i, j, #FFFFFFFF);// this line is not correct
      }
    }
  }
  msk.updatePixels();
  msk.endDraw();
  image(msk, 0, 0);
  image(result, 0, 0);
}

```

Thanks for help !

---

<div class="post-metadata">

### Author: ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)
#### Post date: [September 28, 2023, 3:01pm UTC](https://discourse.processing.org/t/write-color-pixel-to-an-image/42849/2 "2023-09-28T15:01:00Z")

</div>

Your math for msk.pixels is off.

Try `msk.pixels[j * msk.width + i]` instead.
