Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a TIF to PNG in Java?

Under Java what is the best way to go about converting an TIF file to a PNG?

Simplicity is preferable, but if the simplest way is to use a third party library then I would consider that solution.

like image 385
James McMahon Avatar asked Feb 18 '10 19:02

James McMahon


People also ask

How do I convert TIFF to PNG?

How to make your TIFF into a PNG output file. Select File and choose Save As. From the options, select PNG. Choose an interlace option.

How read TIF file in Java?

How to read a TIFF image in Java with ImageIO. Step 1 Download TwelveMonkeys plugin and add to your class path. Step 2 Create a File handle, InputStream, or URL pointing to the raw TIFF image. Step 3 ImageIO will now be able to read a TIFF file into a BufferedImage.


2 Answers

First, install JAI. Then install JAI/ImageIO. Then do

public static void main(final String[] args) throws Exception
{
    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
}
like image 91
Jonathan Feinberg Avatar answered Sep 18 '22 23:09

Jonathan Feinberg


Use imageMagic java libraries like im4java, their performance and quality is much better then JAI

for example:

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

public static void convertTifToPng(File inputImage, File outputImage){
  IMOperation op = new IMOperation();
  op.addImage(); //place holder for input file
  op.addImage(); //place holder for output file

  ConvertCmd convert = new ConvertCmd();
  convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
}

maven dependency for im4java is

<dependency>
  <groupId>im4java</groupId>
  <artifactId>im4java</artifactId>
  <version>0.98.0</version>
</dependency>
like image 30
giladbu Avatar answered Sep 17 '22 23:09

giladbu