We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Using save() with cropping
Page Index Toggle Pages: 1
Using save() with cropping? (Read 1104 times)
Using save() with cropping?
Jun 14th, 2007, 4:02pm
 
I have a simple applet that lets a user input text, renders it to a PG buffer, and then saves the result (it also loads an image that is used as a background for the text input window). Once the user inputs the text and hits return, it automatically renders it to a TIF and closes the applet.

Quote:
import controlP5.*;

ControlP5 controlP5;
PGraphics pg;
PFont font;
public String textValue = "";
PImage backg;

void setup() {
 size(500,200);
 backg = loadImage("back.png");
 pg = createGraphics (2000, 300, JAVA2D);
 controlP5 = new ControlP5(this);
 controlP5.addTextfield("textValue",100,100,200,20);
 controlP5.setColorLabel(0);
 font = loadFont("ArialMT-64.vlw");
 loop();
}
 
void draw() {
 background(255);
 image (backg, 0,0);
 float textwidth = pg.textWidth(""+textValue);
 pg.beginDraw();
 pg.background(255,255,255);
 pg.textFont(font,64);
 pg.text(""+textValue, 0, 64);
 pg.fill(0);
 pg.endDraw();
 if(textwidth > 0) {
   pg.save("text");
   exit();
 }
}


I want to crop the saved image so that only the text is displayed. I know that this should be fairly easy - I have the text width and height, and its position in the buffer. However, I'm not sure the best way to achieve this (apologies for the code as well - it's probably not the greatest way to go about what I want to do (render inputted text to a cropped single line), but I'm quite new to programming in general). I know you can't do this with save() itself - is there another way to achieve the same thing?

Thanks for your help Smiley
Re: Using save() with cropping?
Reply #1 - Jun 14th, 2007, 4:27pm
 
nickjs,

You can save() a portion of the screen using:

Code:

PImage temp = get(x,y,w,h);
temp.save(savePath("filename.ext"));


It should now be possible to calculate the text length depending on the number of letters and send that data to get().

hope that helps.

cheers,
Greg
Re: Using save() with cropping?
Reply #2 - Jun 14th, 2007, 6:12pm
 
Excellent - thanks very much! Because my font wasn't monospaced, I chose to convert textWidth to an integer, and modified the get to grab the graphics buffer rather than the display window.
Quote:
import controlP5.*;

ControlP5 controlP5;
PGraphics pg;
PFont font;
public String textValue = "";
PImage backg;

void setup() {
 size(500,200);
 backg = loadImage("back.png");
 pg = createGraphics (2000, 300, JAVA2D);
 controlP5 = new ControlP5(this);
 controlP5.addTextfield("textValue",100,100,200,20);
 controlP5.setColorLabel(0);
 font = loadFont("ArialMT-64.vlw");  
 loop();
}
 
void draw() {
 background(255);
 image (backg, 0,0);
 pg.beginDraw();
 pg.background(255,255,255);
 pg.textFont(font);
 pg.text(""+textValue, 0, 64);
 float textwidth = pg.textWidth(""+textValue);
 int twint = int(textwidth);
 pg.fill(0);
 pg.endDraw();
 if(textwidth > 0) {
   PImage temp = pg.get(0,0,twint,64);
   temp.save(savePath("text.tiff"));
   exit();
 }
}
Re: Using save() with cropping?
Reply #3 - Jun 14th, 2007, 6:14pm
 
nickjs wrote on Jun 14th, 2007, 6:12pm:
Excellent - thanks very much! Because my font wasn't monospaced, I chose to convert textWidth to an integer, and modified the get to grab the graphics buffer rather than the display window.

Page Index Toggle Pages: 1