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 & HelpPrograms › PNG output hack
Page Index Toggle Pages: 1
PNG output hack (Read 3003 times)
PNG output hack
Jul 1st, 2005, 1:37am
 
Just hacked Koenie and Martin's old PNG/JPEG output hack (now buried in the ALPHA board) to work with 0091 and be more like saveFrame(). Here it is in case anyone wants it.

Code:
// save IMAGE hacked by Martin and Koenie rehacked by Watz
public void saveFrame(String filename) {
BufferedImage img=new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
loadPixels();
img.setRGB(0, 0, width, height, g.pixels, 0, width);

String extn=filename.substring(filename.lastIndexOf('.')+1).toLowerCase();
if(extn.equals("jpg")||extn.equals("png")) { // add here as needed
try {
javax.imageio.ImageIO.write(img, extn, new File(savePath(filename)));
println("saved "+filename);
}
catch(Exception e) {
System.err.println("error while saving as "+extn);
e.printStackTrace();
}
}
else {
super.saveFrame(filename);
}
}
Re: PNG output hack
Reply #1 - Jul 4th, 2005, 3:54pm
 
Great thanks. Exactly what I was looking for/hoping to do.
Re: PNG output hack
Reply #2 - Aug 7th, 2005, 1:30am
 
Is there any way to set the quality of the JPEGs files using this script?

thanks,
martin.
Re: PNG output hack
Reply #3 - Aug 8th, 2005, 1:49pm
 
you could merge the above code with the solution over there. that one is using the JPEGEncoder class which lets you customize the export quality...
Re: PNG output hack
Reply #4 - Aug 8th, 2005, 6:18pm
 
Thanks toxi!
I´ve merged them and it works fine. I don´t know if I have done it in the best way but it works. Here is the code:

Code:

import com.sun.image.codec.jpeg.*;

public void saveFrame(String filename) {
BufferedImage img=new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
loadPixels();
img.setRGB(0, 0, width, height, g.pixels, 0, width);

String extn=filename.substring(filename.lastIndexOf('.')+1).toLowerCase();
if (extn.equals("jpg")) {

  try{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam p = encoder.getDefaultJPEGEncodeParam(img);
    // set JPEG quality to 50% with baseline optimization
    p.setQuality(0.5,true);
    encoder.setJPEGEncodeParam(p);
    encoder.encode(img);
   
    File file = new File(savePath(filename));
    FileOutputStream fo = new FileOutputStream(file);
    out.writeTo(fo);
    println("saved "+filename);
  }
  catch(FileNotFoundException e){
    System.out.println(e);
  }
  catch(IOException ioe){
    System.out.println(ioe);
  }

} else if (extn.equals("png")) { // add here as needed

  try {
    javax.imageio.ImageIO.write(img, extn, new File(savePath(filename)));
    println("saved "+filename);
  }
  catch(Exception e) {
    System.err.println("error while saving as "+extn);
    e.printStackTrace();
  }
 
} else {
  super.saveFrame(filename);
}
}
Page Index Toggle Pages: 1