ImageIO.write - compression

R

Rupert Woodman

Hi,

I have the following code:

String inputFilename = "input.jpg";
String outputFilename = "output.jpg";

BufferedImage bi = ImageIO.read(new File(inputFilename()));
ImageIO.write(bi, "jpg", new File(outputFilename));


The original file is about 1.5 meg in size, the written file is 240kb. What
I don't understand is what changes have been made to the image (I've not
specified any), and how I can control those changes?

Thank you for any thoughts you may have.

rgds

Rupert
 
R

Rupert Woodman

Just to ellucidate a little. I originally had code such as this shown
below. Whatever I pass into the setCompressionQuality() method (i.e. any of
0.05, 0.75, 0.95), results in a written file of 811kb, so I assumed this
code was bad. I don't see why tho.

The BufferedImage passed to this method is created with:

BufferedImage bi = ImageIO.read(new File(c.getInputFileName()));


public void myWrite(BufferedImage bi)
{
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter)writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
System.err.println("getCompressionType: " + param.getCompressionType());

System.err.println("1 getCompressionQuality: " +
param.getCompressionQuality());
param.setCompressionQuality(0.05f);
System.err.println("2 getCompressionQuality: " +
param.getCompressionQuality());

try {
File ff = new File("c:/development/temp/xxx.jpg");
ImageOutputStream ios = ImageIO.createImageOutputStream(ff);
writer.setOutput(ios);
writer.write(bi);
} catch (IOException e) {
e.printStackTrace();
}
}

Many thanks
 
K

Knute Johnson

Rupert said:
Just to ellucidate a little. I originally had code such as this shown
below. Whatever I pass into the setCompressionQuality() method (i.e. any of
0.05, 0.75, 0.95), results in a written file of 811kb, so I assumed this
code was bad. I don't see why tho.

The BufferedImage passed to this method is created with:

BufferedImage bi = ImageIO.read(new File(c.getInputFileName()));


public void myWrite(BufferedImage bi)
{
Iterator writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter)writers.next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
System.err.println("getCompressionType: " + param.getCompressionType());

System.err.println("1 getCompressionQuality: " +
param.getCompressionQuality());
param.setCompressionQuality(0.05f);
System.err.println("2 getCompressionQuality: " +
param.getCompressionQuality());

try {
File ff = new File("c:/development/temp/xxx.jpg");
ImageOutputStream ios = ImageIO.createImageOutputStream(ff);
writer.setOutput(ios);
writer.write(bi);
} catch (IOException e) {
e.printStackTrace();
}
}

Many thanks

Rupert:

I wrote a very similar method a while back with one difference, I delete
the file if it exists. I think I did that because of the problem that
you are having. If the file exists and you write over it it doesn't get
smaller.
 
R

Rupert Woodman

That's interesting - thanks very much for the tip.
I've done that, but changing the compression quality between the 3 allowed
values (0.5, 0.75 & 0.95) results in the same size file - is that a surprise
to you? I expected that if I changed the compression quality to 0.95, I'd
have a larger file that if I used a value of 0.05 (I got that by reading the
Java doc on ImageWriteParam)

Many thanks
 
K

Knute Johnson

Rupert said:
That's interesting - thanks very much for the tip.
I've done that, but changing the compression quality between the 3 allowed
values (0.5, 0.75 & 0.95) results in the same size file - is that a surprise
to you? I expected that if I changed the compression quality to 0.95, I'd
have a larger file that if I used a value of 0.05 (I got that by reading the
Java doc on ImageWriteParam)

Many thanks

Now that I look at your code more carefully, you look to be missing some
pieces. Take a look at the code below.

import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.imageio.plugins.jpeg.*;

public class JPEGWriter {
public static void write(RenderedImage image, float quality,
File file) throws IOException {
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
if (!iter.hasNext())
throw new IOException("No Writers Available");
writer = (ImageWriter)iter.next();
if (file.exists())
file.delete();
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
writer.write(null,new IIOImage(image,null,null),iwp);
ios.flush();
writer.dispose();
ios.close();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top