I need help please (process pictures from a folder)

Hello everybody! I am writing code that processes pictures into letters and I have a problem that the program does not consistently process pictures from a folder even though they are numbered (initially this is a video that was translated into png) tell me how to make it so that it processes them in order and it works finally the right video)

File dir;
File [] files;
int letters = 4;
int rows = 100;
float letterWidth = 256.0/letters;
int bounding = 4;

PImage img;
PShape[] textArray = new PShape[letters];
int frame = 0;

 
 void setup() {
 size(720, 1280);
 noStroke();
 dir = new File(dataPath(""));
 files = dir.listFiles();
 println("Найдено файлов: " + files.length );

 for (int i =0; i<letters; i++) { 
 textArray[i] = loadShape("..//svg//s//"+nf(i+1, 2)+".svg");
 }
 colorMode(HSB);
}
 
void draw() {
 background(250);
 loop();
 if (frame < files.length && files[frame].isFile()) {
 String path = files[frame].getAbsolutePath();
 img = loadImage(path);
 println(" Название кадра: " + frame );
 
 for (int y = height/rows/2; y < img.height; y=y+height/rows ) {
 int x1=0, x2=0;
 int lNum=-1, lNumPre = -1;
 
 
 for (int x = 0; x < img.width; x++) {
 int loc = x + y*img.width;
 float r;
 r = red(img.pixels[loc]);
 if (r>2) {
 lNum = floor(r/letterWidth);
 } else {
 lNum = -1;
 }

 if (lNum != lNumPre ) {
 x2=x;
 printLetter( x1, x2, y, lNumPre);
 x1=x2;
 lNumPre = lNum;
 } else {
 x2=x;
 if (x == width-1) {
 printLetter( x1, x2, y, lNumPre);
 }
 }
 }
 }
 saveFrame("data-out/text-#####.png");
 } else {
 exit();
 }
 frame++;
}

void printLetter(int x1, int x2, int y, int lNum) { 
 if (abs(x2-x1)>bounding*2) {
 lNum = constrain(lNum,0,letters-1);
 //textArray[lNum].disableStyle();
 fill(lNum*letterWidth, 128, 128);
 shape(textArray[lNum], x1+bounding, y-height/rows/2+bounding, abs(x2-x1)-bounding, height/rows-bounding);
 }
}

Not providing an answer but I would like to ask you to post code (in future) using code tags.

To fix your opening post

  1. Edit the post
  2. Select all code
  3. Click the button </>
  4. Save the post

It makes it easier to read, easier to copy and it tells the forum software not to treat e.g. [] as .

[Edited]

@Chrisir, you missed the at the end of the last line :rofl:

1 Like

Hello @Yanina,

The code was formatted for you by a member of our community.

For future reference:
https://discourse.processing.org/faq#format-your-code

There may be something in here to help you:

:)

1 Like