Export PImage file to JSON

Hello @edugimeno,

You can certainly save as a PNG (not a lossy algorithm) and extract the data.
Don’t save as a JPG which is a lossy algorithm!

Example:

/*
 Project:   Save data in a PNG
 Author:    GLV
 Date:      2024-11-12
 Version:   1.0.0
*/

// Reference:
// https://stackoverflow.com/questions/13162839/kinect-depth-image

PImage kin = createImage(320, 240, RGB);

int cols = 320;
int rows = 240;

size(400, 400, P3D);
background(255);

float angleY = TAU/90;
float angleX = TAU/90;

kin.loadPixels();

for (int y = 0; y< rows; y++)
  {
  for (int x = 0; x < cols; x++)
    {
    int loc = x + y*cols;
    float z1 = sin(x*angleX) + sin(y*angleY);  // Range is -2, 2
    int data = (int) map(z1, -2, 2, 0, 2047);
    kin.pixels[loc] = data;
    }
  }

kin.save("data.png");

PImage data = loadImage("data.png");

//image(data, 0, 0);  // If you really want to "see" it!

lights();
translate(width/2, height/2, -150);
rotateX(TAU/8);

for (int y = 0; y< data.height; y+=10)
  {
  for (int x = 0; x < data.width; x+=10)
    {
    int loc = x + y*data.width;
    
    //stroke(255, 0, 0);
    //strokeWeight(5);
    //point (x-160, y-100, kin.pixels[loc]/20+20);

    pushMatrix();
    translate(x-160, y-100+40, (kin.pixels[loc]/20)/2);
    //noStroke();
    fill(128, 255, 0);
    box(10, 10, kin.pixels[loc]/20);
    popMatrix();
    }
  }

Or save and load as a data file with:

I used this for the data:
3D Mesh of Sine Waves from Coding Challenge

:)