Match image to data, sequentially


#1

Hello everyone,

I have an array of numbers from my txt file,
e.g. line[0] = img[0], line[1] = img[1], …

it seems to read all of the images but it only saves one. the code doesn’t give me an error yet it’s not saving multiple images even though when i type println(currentImage) it shows me all of the numbers corresponding to the new array of images. Can someone help me figure out what I’m doing wrong??

p.s. the code below is only addressing where the issue falls, the rest is the functions that are being called.

import java.util.Date;

String[] lines;
int index = 0;

PImage img;
int currentImage = 0;

int mode = 0;

int loops = 1;

int blackValue;
int brigthnessValue;
int whiteValue;

int row = 0;
int column = 0;

void setup() {
  size(200, 200);
  //frameRate(30);

  lines = loadStrings("test.txt");
  String path = sketchPath() + "/draft";

  //println("Listing all filenames in a directory: ");
  String[] filenames = listFileNames(path);
  //printArray(filenames);

  StringList newfilenames = new StringList();
  for (int i = 0; i < filenames.length; i++) {
    if (filenames[i].contains(".png")) {
      newfilenames.push(filenames[i]);
    }
  }

  for (int i = 0; i < lines.length; i++) {

    // Load the new image
    //printArray(path + "/" + newfilenames.get(currentImage));
    img = loadImage(path + "/" + newfilenames.get(currentImage));

      float fftBands = float(lines[i]);

      float imgAlpha = map(fftBands, 10, 2000, 500000, -10000000);  //frequency w/minim lib
      int blackAlpha = floor(imgAlpha);
      image(img, 0,0);

      if (mode == 0) {
        blackValue =  blackAlpha;
      }
    

    row = 0;
    column = 0;

    while (column < width-1) {
      img.loadPixels(); 
      sortColumn();
      column++;
      img.updatePixels();
    }

    while (row < height-1) {
      img.loadPixels(); 
      sortRow();
      row++;
      img.updatePixels();
    }
    // Save the image
    img.save("test-pngs/draft000-####.png");
    currentImage++;
  }
}

#2

@karinalopez87 I don’t have much experience with saving images like this, but i would assume

img.save(“test-pngs/draft000-####.png”);

would just overwrite the image all the time since it’s just one name.
you could use “i” as an identifier to save it as seperate images like:

img.save("test-pngs/draft000-####" + i + ".png");

#3

It seems like a key issue here is the hash-for-numbers scheme that is supported for saveFrame()

You can specify the name of the sequence with the filename parameter, including hash marks (####), which will be replaced by the current frameCount value. (The number of hash marks is used to determine how many digits to include in the file names.) https://processing.org/reference/saveFrame_.html

…and whether that is also supported by PImage.save()

https://processing.org/reference/PImage_save_.html

To program the same behavior yourself you could combine frameCount with nf().

https://processing.org/reference/nf_.html

…so

String frameNumber = nf(frameCount, 4); // same as ####
img.save("test-pngs/draft000-" + frameNumber + ".png");

#4

Perfect, yes, this seemed to work. Thank you!!