I am trying to convert a JPEG image to TIFF. The converted TIFF image is three times larger.
Can someone help me get a TIFF image with the size of the original JPEG?
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageDecoder;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.JPEGDecodeParam;
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFEncodeParam;
import java.io.FileOutputStream;
public class ConvertJPEGtoTIFF{
public static void main(String args[]) throws Exception{
// read input JPEG file
SeekableStream s = new FileSeekableStream("C:\\Testsmall\\Desert.jpg");
JPEGDecodeParam jpgparam = new JPEGDecodeParam();
ImageDecoder dec = ImageCodec.createImageDecoder("jpeg", s, jpgparam);
RenderedImage op = dec.decodeAsRenderedImage(0);
FileOutputStream fos = new FileOutputStream("C:\\Testsmall\\index33.tiff");
TIFFEncodeParam param = new TIFFEncodeParam();
ImageEncoder en = ImageCodec.createImageEncoder("tiff", fos, param);
en.encode(op);
fos.flush();
fos.close();
}
}
As explained by Erwin Bolwidt in a comment:
TIFF is a container format that can contain different kinds of images, compressed or uncompressed. However, you are using the default settings of TIFFEncodeParam
. As you can read in the Javadoc for the class, that means no compression:
This class allows for the specification of encoding parameters. By default, the image is encoded without any compression, and is written out consisting of strips, not tiles.
As a consequence, your TIFF file is much larger than the JPEG file, which uses lossy image compression.
If you want a smaller file size, you must specify a compression (using setCompression
). You can use DEFLATE
for a lossless compression, or JPEG
for JPEG compression (then you should also set JPEG parameters using setJPEGEncodeParam
). The latter should yield a file size similar to the JPEG file.
Note, however, that TIFF is typically used with lossless compression. If you want to use JPEG compression, first check whether the intended recipient of the TIFF files you produce can handle it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With