Home > OS >  Java for loop not stopping. what is wrong with the code
Java for loop not stopping. what is wrong with the code

Time:12-06

It seems like the for loop in my code is not stopping after it iterated througt the counter. it seems to restart, checked with the print function and the length of the directory list seems to be recognized but then it restarts from 0 and so on. First time coding with java and in general i do not have much experience in coding, logic experience comes from visual coding and little bit of python. if somebody can help it would be great.

here is the code, to run it you need will need processing (https://processing.org/download) and some images in the input folder. thougth this is an easy problem, thats why i still post it here.

I know its not pretty so pls dont hate on it

int dim = 1024;
PImage img;
String inDir;
import java.util.*;
import java.text.DecimalFormat;

String outDir;
String nameSpace;
String nameSpaceOut; 
PGraphics pg;

void setup() {
  size(1024, 1024);
  inDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Input 1/";
  outDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Output 3/";
  nameSpace = "ImageToResize";
  pg = createGraphics(dim, dim);

  nameSpaceOut = "Resized";
}

void draw () {
  background(0);

  pg.beginDraw();

  File dir = new File(inDir);
  String[] filenames = dir.list();

  for (int i = 0; i < filenames.length; i  ) {
    background(255, 255, 255);
    String fName = inDir   filenames[i];

    img = loadImage(fName);
    if (img != null) {
      float w = img.width;
      float h = img.height;
      float m = w;
      float f = h;
      if (h > w) { //change to < for crop > for matte
        m = h;
        f = w;
      }
      float factor = (dim/m);
      if(h > w){
        img.resize(int(f*factor), int(m*factor));
      }
      else {
       img.resize(int(m*factor), int(f*factor));
      }

      image(img, width/2-img.width/2, height/2-img.height/2);
      String outName1 = outDir   nameSpaceOut   "_"   i  ".png";
     save(outName1);
     }
   }
}

i try to format pictures which i want to use as a data set input for a GAN machine learnign algorithm.

CodePudding user response:

It looks like your code is stuck in an infinite loop because the draw() function is being called repeatedly by the Processing engine.

Here are a few suggestions to fix your code:

  1. Move the code inside the draw() function to a new function named resizeImages(). This will allow you to call the function only once instead of repeatedly calling it in the draw() function.

  2. Replace the for loop in the resizeImages() function with a while loop. This will allow you to stop looping through the filenames once all the images have been processed.

  3. Inside the while loop, use a counter variable to keep track of how many images have been processed, and stop the loop once all the images have been processed.

Here is an example of how your code could look like with these changes:

int dim = 1024;
PImage img;
String inDir;
import java.util.*;
import java.text.DecimalFormat;

String outDir;
String nameSpace;
String nameSpaceOut; 
PGraphics pg;

// counter variable to keep track of how many images have been processed
int counter = 0;

void setup() {
  size(1024, 1024);
  inDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Input 1/";
  outDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Output 3/";
  nameSpace = "ImageToResize";
  pg = createGraphics(dim, dim);

  nameSpaceOut = "Resized";

  // call the resizeImages() function once
  resizeImages();
}

void draw() {
  // the draw() function does not need to do anything in this case
  // because the images have already been resized in the setup() function
}

// function to resize the images
void resizeImages() {
  pg.beginDraw();

  File dir = new File(inDir);
  String[] filenames = dir.list();

  // while loop to process all the images
  while (counter < filenames.length) {
    background(255, 255, 255);
    String fName = inDir   filenames[counter];

    img = loadImage(fName);
    if (img != null) {
      float w = img.width;
      float h = img.height;
      float m = w;
      float f = h;
      if (h > w) { //change to < for crop > for matte
        m = h;
        f = w;
      }
      float factor = (dim/m);
      if(h > w){
        img.resize(int(f*factor), int(m*factor));
      }
      else {
       img.resize(int(m*factor), int(f*factor));
      }

      image(img, width/2-img.width/2, height/2-img.height/2);
      String outName1 = outDir   nameSpaceOut   "_"   counter  ".png";
     save(outName1);
     }

     // increment the counter variable
     counter  ;
   }
}

CodePudding user response:

int dim = 1024;
PImage img;
String inDir;
import java.util.*;
import java.text.DecimalFormat;

String outDir;
String nameSpace;
String nameSpaceOut; 
PGraphics pg;

void setup() {
  size(1024, 1024);
  inDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Input 1/";
  outDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Output 3/";
  nameSpace = "ImageToResize";
  pg = createGraphics(dim, dim);

  nameSpaceOut = "Resized";
}

void draw () {

  pg.beginDraw();

  File dir = new File(inDir);
  String[] filenames = dir.list();

  for (int i = 0; i < filenames.length; i  ) {
    background(0);
    String fName = inDir   filenames[i];

    img = loadImage(fName);
    if (img != null) {
      float w = img.width;
      float h = img.height;
      float m = w;
      float f = h;
      if (h > w) { //change to < for crop > for matte
        m = h;
        f = w;
      }
      float factor = (dim/m);
      if(h > w){
        img.resize(int(f*factor), int(m*factor));
      }
      else {
       img.resize(int(m*factor), int(f*factor));
      }

      image(img, width/2-img.width/2, height/2-img.height/2);
      String outName1 = outDir   nameSpaceOut   "_"   i  ".png";
     saveFrame(outName1);
     }
   }
}

CodePudding user response:

I see a few issues with your code. First, you are trying to save the image inside the draw() function, which is called repeatedly. This means that the image will be saved multiple times, potentially leading to unexpected behavior.

Additionally, the call to save() should be passed the pg object, not the default renderer. This is because you are drawing the image to the pg graphics object, not the default renderer.

Another issue is that you are using a for loop to iterate over the list of filenames, but you are not using the loop variable i inside the loop. This means that each image will be saved with the same filename.

Lastly, it looks like you are trying to resize and save multiple images, but you are only resizing and saving the last image in the list of filenames. This is because you are overwriting the img variable on each iteration of the loop.

To fix these issues, you should move the code for resizing and saving the image to a separate function, and pass the current filename and loop variable to that function. This will allow you to save each image with a unique filename, and avoid overwriting the img variable.

Here is an example of how you could modify your code to fix these issues:

int dim = 1024;
PImage img;
String inDir;
import java.util.*;
import java.text.DecimalFormat;

String outDir;
String nameSpace;
String nameSpaceOut; 
PGraphics pg;

void setup() {
  size(1024, 1024);
  inDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Input 1/";
  outDir = "C:/Users/Fynn/Desktop/processing-3.5.4/Resizematte/data/Output 3/";
  nameSpace = "ImageToResize";
  pg = createGraphics(dim, dim);

  nameSpaceOut = "Resized";

  // Move this code to a separate function
  File dir = new File(inDir);
  String[] filenames = dir.list();

  for (int i = 0; i < filenames.length; i  ) {
    resizeAndSaveImage(filenames[i], i);
  }
}

// Create a separate function for resizing and saving the image
void resizeAndSaveImage(String filename, int index) {
  background(255, 255, 255);
  String fName = inDir   filename;

  img = loadImage(fName);
  if (img != null) {
    float w = img.width;
    float h = img.height;
    float m = w;
    float f = h;
    if (h > w) { //change to < for crop > for matte
      m = h;
      f = w;
    }
    float factor = (dim/m);
    if(h > w){
      img.resize(int(f*factor), int(m*factor));
    }
    else {
     img.resize(int(m*factor), int(f*factor));
    }

    image(img, width/2-img.width/2, height/2-img.height/2);
    String outName1 = outDir   nameSpace
  • Related