# Dynamic Image Pixel Modulation

**URL:** <https://discourse.processing.org/t/dynamic-image-pixel-modulation/46987>\
**Category:** Gallery\
**Created:** [August 19, 2025, 4:38pm UTC](https://discourse.processing.org/t/dynamic-image-pixel-modulation/46987 "2025-08-19T16:38:46Z")\
**Posts on this page:** 1\
**Page:** 1

<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:** [August 19, 2025, 4:38pm UTC](https://discourse.processing.org/t/dynamic-image-pixel-modulation/46987/1 "2025-08-19T16:38:46Z")

</div>

Hello folks!

Inspired by:  
_[Using Pixels[] Array over Points • trying to move Pixels(XY) off screen like Points](https://discourse.processing.org/t/using-pixels-array-over-points-trying-to-moving-pixels-xy-off-screen-like-points/24309)_

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/5/e/5e999df2809643454a33210a793548522a6aac08.jpeg)

```processing
// Dynamic Image Pixel Modulation
// v1.0.1
// GLV 2025-8-19
//
// Inspired by topic:
// https://discourse.processing.org/t/using-pixels-array-over-points-trying-to-moving-pixels-xy-off-screen-like-points/24309

PImage img, img2, img3;
float mod;
int modIndex;
int choice = 1;

int sz; // This is for width and height

void settings()
  {
  sz = (int) (255/cos(radians(45))); 
  println(sz);
  size(sz, sz, P2D);
  }

void setup() 
  {
  //size(400, 400, P2D); // Moved to settings
  frameRate(60);
  img = loadImage("http://learningprocessing.com/code/assets/sunflower.jpg");
  img.loadPixels(); 
  println(img.width, img.height);
  background(0);
  
  img2 = createImage(sz, sz, ARGB);
  img2.loadPixels();
  for (int i = 0; i < img2.width*img2.height ; i++)
    {
    img2.pixels[i] = color(0, 255);
    }
  img2.updatePixels();
  }

void draw() 
  {  
  //Sinusoidal modulation from 0 to 2
  mod = 1+1*sin(modIndex*TAU/500-TAU/4);
  
  switch(choice) 
    {
    case 1: 
      //println("Plot 1");
      plot1();
      break;
    case 2: 
      //println("Plot 2");
      plot2();
      break;
    case 3: 
      //println("Plot 3");
      plot3();
      break;
    case 4: 
      //println("Plot 4");
      plot4();
      break;      
    default:
      println("Woops!"); 
      break;
    }
  modIndex++;
  surface.setTitle("Plot: " + choice + " FRate: " + nf(frameRate,0,1));
  }
  
// **********************************************************************************

void plot1()
  {
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);  
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.get(x, y);
      float sz = (brightness(c)/255)*mod;
      set(int(x*sz), int(y*sz), c);      
      }
    }  
  }

// **********************************************************************************

void plot2()
  {  
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);  
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = (brightness(c)/255)*mod;
      set(int(x*sz), int(y*sz), c);
      }
    }
  }

// **********************************************************************************  
  
void plot3()
  {
  fill(0, 5);
  noStroke();
  rect(0, 0, width, height);    
    
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = (brightness(c)/255)*mod;
      //sz = 1; // Try this to see original image.
      img2.pixels[int(x*sz)+int(y*sz)*img2.width] = c;    
      }
    }
  img2.updatePixels();  
  image(img2, 0, 0);     
  }
  
// **********************************************************************************  
  
void plot4()
  {
  // Sets pixel array to a fixed color with transparency  
  for (int i = 0; i < img2.width*img2.height ; i++)
    {
    img2.pixels[i] = color(0, 5);
    }
  
  for (int y = 0; y < img.height; y++ ) 
    {
    for (int x = 0; x < img.width; x++ ) 
      {
      // Pixel location and color
      color c = img.pixels[x+img.width*y];
      float sz = brightness(c)/255*mod;
      //sz = 1; // Try this to see original image.
      //c = color(255, 255, 0); //Test with fixed color
      img2.pixels[int(x*sz)+int(y*sz)*img2.width] = c; // x + y*width     
      }
    }
  img2.updatePixels();  
  image(img2, 0, 0);    
  }  
  
void keyPressed()
  {
  modIndex = 0;
  
  //if (choice>4) choice = 1;
  //choice++;
  
  choice = choice%4;
  choice++;
  }

```

`:)`
