Bug 342 : add ability to control jpeg compression level with save() and saveFrame()
Last modified: 2007-02-03 09:59




Status:
ASSIGNED
Resolution:
-
Priority:
P3
Severity:
enhancement

 

Reporter:
fry
Assigned To:
fry

Attachment Type Created Size Actions

Description:   Opened: 2006-05-10 07:06
this is extremely low priority, since there are now a range of possible
image formats, but the following code will handle changing the jpeg
compression level, and was originally contributed here:
http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Programs;action=display;num=1120174647

the main reason for holding off on this is that 1) it's only possible with
1.4+ and 2) it means changing or adding API. it's not just save() in
PImage, but also in PApplet and saveFrame() in PApplet that would all need
an optional parameter. it seems as though it would make things needlessly
complicated, and might be better served by a simple example.

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);
}