How can I reduce the memory chunk this code demands?

edited February 2017 in Raspberry PI

Hello, I am very new to Processing. I have been working towards a digital picture frame idea using Raspberry Pi, and I would like some HD still images to cycle in a loop, ideally with a fade.

I have some code I have scrubbed together, it works on my Mac but kills my Raspberry Pi with a not enough memory error. I can see with activity monitor on my Mac that the program does indeed eat 600mb of ram! I am just cycling 5 images.

The Pi can't get past the second line in Void setup() where the image is scaled with .resize. Tried adding some delays to no effect

I know it could be simplified, but had to fudge this together to get it to work - I have been working a little with the Image Handling tutorials on this site, but i'm not sure how useful getting into the pixel manipulation will be.

PImage img1, img2, img3, img4, img5;
int a = 0;
int imgCounter = 0;

void setup() {
  size(640, 480); 
  img1 = loadImage("001.jpg");
  img1.resize(width, height);
  img2 = loadImage("002.jpg");
  img2.resize(width, height);
  img3 = loadImage("003.jpg");
  img3.resize(width, height);
  img4 = loadImage("004.jpg");
  img4.resize(width, height);
  img5 = loadImage("005.jpg");
  img5.resize(width, height);

  frameRate(30);
  delay(1000);
}

void draw() {
  background(0);
  tint(255, a);
  if(imgCounter == 0) {
  background(img5);  
  image(img1, 0, 0);
  }
  else if(imgCounter == 1) {
    background(img1);
    image(img2, 0, 0);
  }
  else if(imgCounter == 2) {
    background(img2);
    image(img3, 0, 0);
  }
  else if(imgCounter == 3) {
    background(img3);
    image(img4, 0, 0);
  }
  else if(imgCounter == 4) {
    background(img4);
    image(img5, 0, 0);
  }
  a++;
  if(a == 255) {
    imgCounter++;
    a = 0;
  }

  if(imgCounter == 5) { 
    imgCounter = 0;
  }

}

Answers

  • How big are are the images (in pixels)?

    They are decoded during loading into processing into raw bytes, 4 bytes per pixel, so it soon mounts up.

    Also, I would make the images the correct size using a graphics package and save the results in data directory, not load originals and then resize them.

  • 5 images at 640x480 x 4 bytes per pixel = 6,144,000 bytes for the data... Seems more reasonable.

  • Right, I thought that might be the case :) Thank you!

Sign In or Register to comment.