How to specify output size

I’m probably staring at the obvious answer but can’t see it.
Down below id the simple code I’ve put together that reads this simple list and then creates a folder based on the contents of the list, in the folder is another folder which has one file which contains the event date / age group / location as a jpeg. it’s pretty much working as I intend, except it only writes the output image at a size of 100*100. I’ve tried many ways o specify an image size, I’m even loading a starting image of quite a large size.
Please can anyone let me know which bit of code I need to add, and where, so I can make the output as I want?
Here is the input text file…

October 2019
On course
Finish Straight
Yr3-Girls
Yr3-Boys
Yr4-Girls
Yr4-Boys
Yr5-Girls
Yr5-Boys
Yr6-Girls
Yr6-Boys
Yr7-Girls
Yr7-Boys

And here is the code.

PImage img;  // Declare variable "a" of type PImage
String entry; // this is used for folder name
String outfile; //this is used for jpeg extension
String eventdate;
String location1;
String location2;
String filepath;
String quotemark = "\"";
String backslash = "/";
PFont f = createFont("Arial",16,true);
PGraphics pg;



String[] lines = loadStrings("textinput");
//println("there are " + lines.length + " lines");
eventdate = lines[0];
location1 = lines [1];
location2 = lines[2];
for (int i = 3 ; i < lines.length; i++) {
  
  entry = lines[i];
  
  outfile = entry + ".jpg";
  filepath = "banners";
  filepath=filepath + backslash + entry + backslash + outfile;
 // println(filepath);
//  save(filepath);
// now we have created the file why not open it, add text and save it again
// problem is that everything being saved at 100*100

// repeat the code below for no location eg seniors and 2 locations

img = loadImage("blank2.JPG");
//img = createImage(4000,4000,RGB);
image (img,0,0);
text (eventdate,10,15);
text (entry,10,70);
save (filepath);
//group above got the no location
//now for 'on course'
outfile = entry + "on-course.jpg";
  filepath = "banners";
  filepath=filepath + backslash + outfile + backslash + outfile;
 // println(filepath);
//  save(filepath);
// now we have created the file why not open it, add text and save it again
// problem is that everything being saved at 100*100

// repeat below for no location eg seniors and 2 locations
img = loadImage("blank2.JPG");
//img = createImage(4000,4000,RGB);
image (img,0,0);
text (eventdate,10,15);
text (entry,10,50);
text(location1,10,70);
save (filepath);

//now for finishing straight
outfile = entry + "straight.jpg";
  filepath = "banners";
  filepath=filepath + backslash + outfile + backslash + outfile;
  println(filepath);
//  save(filepath);
// now we have created the file why not open it, add text and save it again
// problem is that everything being saved at 100*100

// repeat below for no location eg seniors and 2 locations
img = loadImage("blank2.JPG");
//img = createImage(4000,4000,RGB);
image (img,0,0);
text (eventdate,10,15);
text (entry,10,50);
text(location2,10,70);
save (filepath);


}
1 Like
save()

is used to save the canvas,
yours is the default 100*100

as you not use

String outfile = "data/myCanvas.jpg";

void setup() {
  size(500,500);
}

void draw(){}

void keyPressed() {
  if ( key == 's' ) save(outfile);
}

what gives a 500*500 empty picture.
but wait:
you could try

String outfile1 = "data/myCanvas.jpg";
String outfile2 = "data/img.jpg";
String infile  = "data/moonwalk.jpg";
PImage img;

void setup() {
  size(500,500);
  img = loadImage(infile);
}

void draw(){
  //image(img,0,0);
}

void keyPressed() {
  if ( key == 's' ) save(outfile1);   // canvas
  if ( key == 'c' ) img.save(outfile2);  // copy img
}

what gives here a copy 640*360 but smaller in kB ( jpg compression?)

1 Like

Hello kll,
Thank you for the ideas there, I have been trying similar approaches with various error messages. ‘unexpected tokens’ ‘end of file’ and other errors.
It seems absolutely right that it’s a default size issue.
In my code, where would you add either of the suggestions you’ve posted
Many thanks
Mike

// save (filepath);
img.save(filepath);

test
i understand that that would copy one picture to a other location

but please why not restart your project
and make a good processing setup
incl a show on the canvas…

That’s a fair comment about the ‘show’ - however, this is one of those tasks that I really just want to create the folders and files as simply and rapidly as possible.
Processing.org is doing a remarkable job, it would be really something to get the image size to a size I specify.
I’'l try your latest suggestion, thank you for your help :):slightly_smiling_face:

no problem,
between

img = loadImage(infile);
// here
img.save(outfile);

can use

example:

String outfile1 = "data/myCanvas.jpg";
String outfile2 = "data/img.jpg";
String outfile3 = "data/imgnewsize.jpg";
String infile  = "data/moonwalk.jpg";
PImage img;

void setup() {
  size(500,500);
  img = loadImage(infile);
}

void draw(){
  //image(img,0,0);
}

void keyPressed() {
  if ( key == 's' ) save(outfile1);
  if ( key == 'c' ) img.save(outfile2);
  if ( key == 'r' ) {
    img.resize((int)(img.width*2),(int)(img.height*2) );
    img.save(outfile3);
  }
}

1 Like

Thank you so much for your help, I will try that later today or tonight

1 Like

Hello,

This was my morning programming exercise! Thanks!

I made a few changes to test the code and quality of output.

You can use PGraphics which does not depend on size of canvas.
I had to add setup() for PGraphics and I only added draw() to display “Done!”.
I provided a template for that; you may add the ? to complete if you wish.

Reference:
https://processing.org/reference/PGraphics.html

//https://discourse.processing.org/t/how-to-specify-output-size/14711

  String entry;         // this is used for folder name
  String outfile;       //this is used for jpeg extension
  String eventdate;
  String location1;
  String location2;
  String filepath;
  String quotemark = "\"";
  String backslash = "/";
  PImage img;
  int count = 3;
  int w = 1000;
  int h = 400;
  
  PGraphics pg;

void setup()
  {
  size(1000, 400);
  fill(0);
  textAlign(CENTER);
  textSize(96);
  
  String[] lines = loadStrings("textinput");
  logo();
  
  pg = createGraphics(1000, 400);
  //pglogo();
  }
  
void draw()
  {
  background(255, 0, 0);
  text("Done!", width/2, height/2);
  noLoop();
  }    

void logo()
  {
  String[] lines = loadStrings("textinput");
  eventdate = lines[0];
  location1 = lines[1];
  location2 = lines[2];  
    
  for (int i = 3; i < lines.length; i++) 
    {
    background(255);
    entry = lines[i];
    
    outfile = entry;
    filepath = "banners";
    filepath=filepath + backslash + entry + backslash + outfile;
    text (eventdate + "\n\r" + entry + "\n\r", width/2, 150);
    
    save(filepath + ".gif");
    save(filepath + ".jpg");
    save(filepath + ".png");
    }
  }
  
/*
void pglogo()
  {
  String[] lines = loadStrings("textinput");
  eventdate = lines[0];
  location1 = lines[1];
  location2 = lines[2];  
    
  for (int i = 3; i < lines.length; i++) 
    {
    background(255);
    entry = lines[i];
    
    outfile = entry;
    filepath = "pgbanners";
    filepath=filepath + backslash + entry + backslash + outfile;
    
    pg.beginDraw();
    pg.background(?);
    pg.fill(?);
    pg.textSize(?);
    pg.textAlign(?);
    pg.text (?);
    pg.endDraw();
    
    pg.save(?);
    pg.save(?);
    pg.save(?);
    
    //image(pg, 0, 0);
    }
  }
*/

File sizes same for both versions:
image

I suggest using PNG (my first choice) or GIF for this example; JPG has lossy compression and in this case a larger file.
A search for “jpg vs gif vs png” will discuss this topic.

:slight_smile:

1 Like

I am absolutely astounded at the help and support on this forum - I use a few facebook groups which are full of kind and helpful people, but you good folks are truly, truly amazing and I am so grateful for your help!!

3 Likes