# Colouring pixels

**URL:** <https://discourse.processing.org/t/colouring-pixels/15551>\
**Category:** Coding Questions\
**Created:** [November 16, 2019, 8:19pm UTC](https://discourse.processing.org/t/colouring-pixels/15551 "2019-11-16T20:19:00Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [November 17, 2019, 2:45pm UTC](https://discourse.processing.org/t/colouring-pixels/15551/21 "2019-11-17T14:45:28Z")

</div>

NOW it works! It was the missing img from img.updatePixels();  
I had been trying with and without.  
Thank you!  
(What do you think of my proposed workflow (see reply to kll).)

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 17, 2019, 2:46pm UTC](https://discourse.processing.org/t/colouring-pixels/15551/22 "2019-11-17T14:46:23Z")

</div>

Something I was trying the other day:

```auto
PImage img;
String filename = str(month()) + str(day());

void setup() 
  {
  size(500, 500); //I want a white margin. This is currently same size as image.
  background(255);
  noLoop();
  make();
  }

void draw() 
  {
  background(0);
  image(img, (width-275)/2, (height-275)/2);
  println(frameCount);
  }
  
void make()
  {
  img = loadImage("blank.png");
  //img.width = 275;
  //img.height = 275;
  println(img.width);
  println(img.height);
  colorMode(RGB, 255, 255, 255);
  int offset = (img.width-255)/2; //to center it
 
  for (int y = offset; y < img.width-offset; y++ ) 
    {
    for (int x = offset; x < img.height-offset; x++ ) 
      {
      int loc = x + y*img.width;
      img.pixels[loc] = color(loc%img.width, (loc - (loc%img.width))/img.width, 0); //
      }
    }

  img.updatePixels();
  img.save(filename + ".jpg");
  
  println("done");  
  }

```

This is the “blank.png” image (275x275):  
 ![blank](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/49c2c115c6193c5af6a26c8f8b252e3c3da789b3.png)  
And output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/68176dee26f23d79f5fdfb9aa830511c72f9dc8c.png)

🙂

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 17, 2019, 2:48pm UTC](https://discourse.processing.org/t/colouring-pixels/15551/23 "2019-11-17T14:48:16Z")

</div>

Please save this under a new file name so that you have a working version

Then work on with a new version

The proposal is homework and I wish you good luck Sir.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [November 17, 2019, 3:00pm UTC](https://discourse.processing.org/t/colouring-pixels/15551/24 "2019-11-17T15:00:47Z")

</div>

Yes, all saved as a new version with setup() and draw() as they should be.

Shall I continue with loading an image or move over to createImage() ?

The rest, as you say, is homework.

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [November 17, 2019, 3:05pm UTC](https://discourse.processing.org/t/colouring-pixels/15551/25 "2019-11-17T15:05:56Z")

</div>

Thank you. That works as well. Now I need to study it!

```auto
  img.width = 255;
  img.height = 255;

```

---

<div class="post-metadata">

**Author:** ![paulstgeorge](https://avatars.discourse-cdn.com/v4/letter/p/53a042/32.png) [@paulstgeorge](https://discourse.processing.org/u/paulstgeorge)\
**Post date:** [November 18, 2019, 8:15am UTC](https://discourse.processing.org/t/colouring-pixels/15551/26 "2019-11-18T08:15:50Z")

</div>

Please tell me more.

[Previous page](https://discourse.processing.org/t/colouring-pixels/15551.md?page=1)
