Save image by input name as jpg or to absolute path

I am inputting a list of names and output images made as result of list of names. This works fine . It will out put them as tif . But how can I get them output as jpg. and also to a folder in the sketch folder rather than the sketch folder itself. Lines 98 99 100.

   //This now adds the rates to the pic;
    int[] colorLines;
    int[] red;
    int[] green;
    int[] blue;
   int n;
   int r1;
   int g1;
   int b1;
     
    String[] lines;
     String l;
    
    void setup(){
      size(400,200);}
      
void draw() {
  listOfRates();
 

noLoop();
 
}
void  listOfRates(){
  int[] colorLines = new int [400];
    int[] red = new int [400];;
    int[] green =new int [400];;
    int[] blue =new int [400];
    PGraphics pg;
  
  String[] lines = loadStrings("listin.txt");
 
  println("there are " + lines.length + " lines");

   for (int i = 0 ; i < lines.length;) 
   {
      String line= lines[i];
       String[] linesrates = split(line,' ');
       String last = linesrates[linesrates.length-1];
       String[] notlast = shorten(linesrates);
       String first = join(notlast," ");
      println(line); 
      println(last);
      println(first);
      
    
 pg = createGraphics(400,200);
    pg.beginDraw();
      // Populate the colorlines with widths .
      for( int k=0; k < colorLines.length; k++)
      { 
        strokeCap(SQUARE);
   
         int sW = int(random(20,40));
        //strokeWeight(sW);       
      colorLines[k] = sW;
      }
      //Make each line.
      for( int m = 0; m < 400; m+=16 )
      {
    // Give each line a random color and width each time it's drawn.
  

   float r = int(random(255));

    red[m]=int(r);
  
    float g = int(random(255));
    
    green[m] = int(g);
    float b = int(random(255));

    blue[m] = int(b);//blue
      }
          

   //pg = createGraphics(400,200);
    //pg.beginDraw();
    for (int j = 0; j < 400; j += 16 )
  { 
  strokeWeight(colorLines[j]);
   stroke(red[j],green[j],blue[j]);
   line(j, 0,  j , 200); 
  //put input on top of colorcode
  }
     textSize(26);
     fill(255,255,255);
      text(first,50,50);
      textSize(10);
    text("AM", 365, 185);
     textSize(14);
    text("rate = " + last, 50, 185);
     pg.endDraw();
     
      PImage partialSave = get(0,0,400,200);
     
       partialSave.save(line);
    
   
    println("screenshot saved");

    //text("done",10,20);
    
    i++;
}//noLoop();
return;
}

give a name and pls
use “data/filename”
for load and save!!!

the default load reads from data/
the default save writes to sketch path
that is more confusing as it helps.
so use path and filename.

/**
 * Load and save 
 */
PImage img, part;
void setup() {
  size(640, 360);
  img = loadImage("data/moonwalk.jpg"); 
  part = img.get(0,0,width/2,height/2);
  part.save("data/get_top_left.jpg");
  exit();
}

//void draw() {}

Pass the filename’s String as an argument for dataPath(): :open_file_folder:

partialSave.save(dataPath(line));

1 Like

thanks Learned something there. But how to save as jpg. in my sketch the input is loadStrings and out put is Images with the stringname.(that is input).

i have no idea what is the content of String line
( you not show the file or the console print )
but you can do like:

String saveto,line;



  line = "picname";
  saveto = "data/"+line+".jpg";
  part.save(saveto);

even:

String saveto,line;
int p;

  line = "picname";
  p++;
  saveto = "data/"+line+"_"+p+".jpg";
  println(saveto);
  part.save(saveto);

yep. that works. great thanks But I dont understand this line; ```
saveto = “data/”+line+".jpg";

play with it and print it, so you can learn, like

saveto = "data/"+line+"_"+p+".jpg";

constructs a path/filename from strings, string variables and integer( like counter )
i think its easy to understand, if you need it i don’t know? as i am still guessing
about your code.

1 Like