Taking Screenshots after a key is pressed

Hi. I'm really stuck. I'm wanting to use saveFrame() to make it so that the user can take screenshots on any page but I'm really confused at how I would go about placing this in my code (below). I want it so that when the S key is pressed, a screenshot is saved and sent to a folder where I can change the file name and file type to png. I've looked on the forum for similar problems but haven't been able to get this function to work.

PImage springImage;                  // This stores the Spring image.
PImage summerImage;                  // This stores the Summer image.
PImage autumnImage;                  // This stores the Autumn image.
PImage winterMain;                   // This stores the Winter image.
PImage winterBorder;                 // This stores the border for the Winter image.
boolean insideSummer = false;        //
boolean insideAutumn = false;
boolean insideWinter = false;
boolean insideSpring = false;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int bx=10;                           // Position of the "spring" button on the x axis.
int bxx=250;                         // Position of the "summer" button on the x axis.
int bxxx=500;                        // Position of the "autumn" button on the x axis.
int bxxxx=750;                       // Position of the "winter" button on the x axis. 
int by=630;                          // Position of the buttons on the y axis.
int bw=250;                          // The width of the buttons.
int bh=120;                          // The height of the buttons.

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

int quantity = 300;
float [] xPosition = new float[quantity];
float [] yPosition = new float[quantity];
int [] flakeSize = new int[quantity];
int [] direction = new int[quantity];
int minFlakeSize = 1;
int maxFlakeSize = 5;


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void setup() {                        
  size (1000,750);                      // Size of canvas.
 springImage = loadImage("spring.png");        //Loading the spring image.
 summerImage = loadImage("summer.png");        //Loading the summer image.
 autumnImage = loadImage("autumn.png");        //Loading the autumn image.  
 winterBorder = loadImage("winterborder.png");        //Loading the winter image.
 winterMain = loadImage ("wintermain.png");

  size(1000, 750);
  frameRate(30);
  noStroke();
  smooth();

  for(int i = 0; i < quantity; i++) {
    flakeSize[i] = round(random(minFlakeSize, maxFlakeSize));
    xPosition[i] = random(0, width);
    yPosition[i] = random(0, height);
    direction[i] = round(random(0, 1));
  }

}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


void draw()
{


  fill(255, 0, 0, 0);          
  background(springImage);    

  if(insideSpring==true){
    background(springImage);}

  if(insideSummer==true){
    background(summerImage);} 

  if(insideAutumn==true){
      background(autumnImage);}

  if(insideWinter==true){
    image(winterMain, 0, 0);


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

fill(255);
  for(int i = 0; i < xPosition.length; i++) {

    ellipse(xPosition[i], yPosition[i], flakeSize[i], flakeSize[i]);

    if(direction[i] == 0) {
      xPosition[i] += map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
    } else {
      xPosition[i] -= map(flakeSize[i], minFlakeSize, maxFlakeSize, .1, .5);
    }

     yPosition[i] += flakeSize[i] + direction[i]; 

    if(xPosition[i] > width + flakeSize[i] || 
    xPosition[i] < -flakeSize[i] || yPosition[i] > height + flakeSize[i]) {
      xPosition[i] = random(0, width);
      yPosition[i] = -flakeSize[i];
    }

  }

}
    noFill();

    if(insideWinter==true){
     image(winterBorder, 0, 0);}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  noStroke();
  rect(bx,by,bw,bh); // Our Button in the position (400,300) with size of 60 pixels by 60 pixels
  rect(bxx, by, bw, bh);
  rect(bxxx, by, bw, bh);
  rect(bxxxx, by, bw, bh);

if(mousePressed){
  if(mouseX > bx && mouseX < bx+bw && mouseY > by && mouseY < by+bh){
    insideSpring=true;
    insideAutumn=false;
    insideWinter=false;
    insideSummer=false;}

}


if (mousePressed){
  if(mouseX > bxx && mouseX < bxx+bw && mouseY > by && mouseY < by+bh){
    insideSummer=true; 
    insideAutumn = false;
    insideWinter = false;
    insideSpring = false;}         // condition to know if we do clik inside the button
}
if (mousePressed){
  if(mouseX > bxxx && mouseX < bxxx+bw && mouseY > by && mouseY < by+bh){
    insideAutumn=true ; 
    insideSummer = false;
    insideWinter = false;
    insideSpring = false;}
}

if (mousePressed){
  if(mouseX > bxxxx && mouseX < bxxxx+bw && mouseY > by && mouseY < by+bh){
    insideWinter=true;
    insideSummer=false;
    insideAutumn=false;
    insideSpring=false;}
}

{
println("autumn is "+insideAutumn);
println("summer is "+insideSummer);
println("winter is "+insideWinter);
println("spring is "+insideSpring);
}}
Tagged:

Answers

  • edited November 2016
    void keyPressed(){
        if(key=='s'||key=='S')
                saveFrame(); 
    }
    

    or use

      // Saves each frame as line-000001.png, line-000002.png, etc.
      saveFrame("line-######.png");
    

    in the code above

    see

    https://www.processing.org/reference/saveFrame_.html

  • It says here "It is possible to specify the name of the sequence with the filename parameter and make the choice of saving TIFF, TARGA, PNG, or JPEG files with the ext parameter." - Not quite sure how to do that?

  • just use the filename with the right ending

    Parameters
    filename String: any sequence of letters or numbers that ends with either ".tif", ".tga", ".jpg", or ".png"

  • Append a file extension, to indicate the file format to be used: either TIFF (.tif), TARGA (.tga), JPEG (.jpg), or PNG (.png). Image files are saved to the sketch's folder, which may be opened by selecting "Show Sketch Folder" from the "Sketch" menu.

  • Thank you for your help, it worked! Currently just trying to figure out how to save multiple images with different names, as right now it only allows me to save one image with the file name of 'example.png' and this file is just replaced every time I take a new screenshot.

  • edited November 2016

    really? It should use the frame number? How many times draw() ran?

    you need to use ###### which gets replaced with frame number

    // Saves each frame as line-000001.png, line-000002.png, etc.
    saveFrame("line-######.png");
    
  • How would I assign the S key using that method?

  • edited November 2016 Answer ✓
        void keyPressed(){
            if(key=='s'||key=='S'){
    
                // Saves each frame as line-000001.png, line-000002.png, etc.
                saveFrame("line-######.png");
    
            }
        }
    
  • Ahhh okay, that works! Thank you. If for example I was wanting to change this in future, to save images that have a completely unique file name that are based on say the time for example (the time taken included in the file name, or the date etc) do you know if this is possible?

  • // for calendar / time stamp for files to save
    import java.util.*;
    
    void setup() {
      println ( timestamp() + ".png") ;
    }
    
    String timestamp() {
      Calendar now = Calendar.getInstance();
      return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
    }
    //
    
  • there is probably a simpler solution like

    year()+month()+day()+"_"+hour()+minute()+second()

    or so

    see section "Time & Date" on

    https://www.processing.org/reference/

Sign In or Register to comment.