# Export PImage file to JSON

**URL:** <https://discourse.processing.org/t/export-pimage-file-to-json/45321>\
**Category:** Libraries\
**Created:** [November 11, 2024, 7:48pm UTC](https://discourse.processing.org/t/export-pimage-file-to-json/45321 "2024-11-11T19:48:08Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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 12, 2024, 11:45am UTC](https://discourse.processing.org/t/export-pimage-file-to-json/45321/5 "2024-11-12T11:45:06Z")

</div>

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:

```auto
/*
 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();
    }
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/1/e10dfc28d0e409df433ac91b053af76686a5ac9e.png)

Or save and load as a data file with:

> **[saveBytes() / Reference](https://processing.org/reference/savebytes_)**
>
> As the opposite of loadBytes(), this function will write an entire array of bytes to a file. The data is saved in binary format. This file is saved to the sketch's folder, which is opened by se…

> **[loadBytes() / Reference](https://processing.org/reference/loadbytes_)**
>
> Reads the contents of a file and places it in a byte array. If the name of the file is used as the parameter, as in the above example, the file must be loaded in the sketch's "data" directory/folder. …

I used this for the data:  
_[3D Mesh of Sine Waves from Coding Challenge](https://discourse.processing.org/t/3d-mesh-of-sine-waves-from-coding-challenge/22806)_

`:)`

---

_[View the full topic](https://discourse.processing.org/t/export-pimage-file-to-json/45321)._
