3

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();    
}     
}     
sleske
  • 81,358
  • 34
  • 189
  • 227
  • This has to do with how the various formats compress images. Typically you'll find it hard to the to perform comparably because of the different approaches they take – MadProgrammer Mar 13 '17 at 06:19
  • 4
    TIFF is a very open container format that can do compression and encoding in many different ways. You can also use JPEG compression inside TIFFs (although typically TIFFs are use with lossless compression, which doesn't compress as well as lossy compression algorithms like JPEG). See for example: http://stackoverflow.com/questions/6760524/jpeg-in-tiff-encoding – Erwin Bolwidt Mar 13 '17 at 06:40
  • @ErwinBolwidt: Yes, exactly. I took the liberty of expanding your comment into an answer, hope you don't mind. – sleske Mar 13 '17 at 07:56
  • @sleske No problem, thanks. Didn't have time for a full answer. – Erwin Bolwidt Mar 13 '17 at 08:20

2 Answers2

3

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.

sleske
  • 81,358
  • 34
  • 189
  • 227
0

You can control the quality by setting the compression quality level. Unfortunately converting the files this way is lossy. The ideal way is to find a program that does not decode then re-encode the jpeg data but rather transfers it to the tiff format without modifying the compression. I have not seen such a utility. I am not sure how to set the quality in your coding case, but you could try "jpeg:75" or "jpeg:50" in place of just "Jpeg" for compression (I am basing this on the tiffcp command).

Rick J
  • 1
  • 2