Convert PPM to JPG or BMP in java


I need to know how feasible is it to write a function in java to read a ppm file and convert it to jpg or bmp format. Anyone has experience with this? I am able to achieve the goal using tools such as ImageMagick but I want to do it in pure Java way.


I would search for ImageMagick Application Programmer Interfaces. They have interfaces for every significant language.

I find the Java philosophy is to extensively research what already exists, find the best solution for your needs, then write the minimal code needed to interface to it. This is a pure Java way.


Beside the old Sun JAI implementation there are additional ImageIO plugins <a href="https://github.com/haraldk/TwelveMonkeys" rel="nofollow">TwelveMonkeys ImageIO</a> which extend the ImageIO implementation in the JDK/JRE.

Below is a small example using those plugins. The example depends on version 3.1-SNAPSHOT (earlier release version does not provide PNM support). So you need to build the plugin project first.


<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>sub.optimal</groupId> <artifactId>JAI-Demo</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.twelvemonkeys.imageio</groupId> <artifactId>imageio-pnm</artifactId> <version>3.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.twelvemonkeys.imageio</groupId> <artifactId>imageio-jpeg</artifactId> <version>3.1-SNAPSHOT</version> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.3.2</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <killAfter>-1</killAfter> <mainClass>sub.optimal.jai.Main</mainClass> <systemProperties> <systemProperty> <key>user.dir</key> <value>${project.build.directory}\classes</value> </systemProperty> </systemProperties> </configuration> </plugin> </plugins> </build> </project>


package sub.optimal.jai; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Arrays; import javax.imageio.ImageIO; /** * * @author SubOptimal */ public class Main { public static void main(String[] args) throws IOException { String outFormat = "%-17s: %s%n"; String filesDirectory = System.getProperty("user.dir"); System.out.printf(outFormat, "files directory", filesDirectory); System.out.printf(outFormat, "supported formats", Arrays.toString(ImageIO.getWriterFormatNames())); Path inputFile = Paths.get(filesDirectory, "pond.ppm"); System.out.printf(outFormat, "input file", inputFile.toAbsolutePath()); InputStream is = Files.newInputStream(inputFile, StandardOpenOption.READ); BufferedImage image = ImageIO.read(is); File outputFile = Paths.get(filesDirectory, "output.jpg").toAbsolutePath().toFile(); System.out.printf(outFormat, "output file", outputFile.getAbsolutePath()); boolean writeSuccess = ImageIO.write(image, "JPEG", outputFile); System.out.printf(outFormat, "write successful", writeSuccess); } }

<strong>./src/main/resources/pond.ppm</strong><br /> The file is one of the images provided in <a href="http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-client-419417.html#7529-JAI-1.1.2-demo-oth-JPR" rel="nofollow">jai-1_1_2-unix-sample.tar.gz</a>

Build and run the example code.

mvn clean compile exec:java


  • JENKINS: ERROR when I try to use an older JDK for a specific maven project
  • Grails 3 - How to publish to Artifactory
  • Regarding client side code generation from WSDL
  • maven jboss-as:start A required class was missing … org/sonaty…/ArtifactResolutionException
  • How do you create a Fuseki SPARQL server using the Apache Jena Java API?
  • XSD with multi occurrences unordered
  • “mvn clean generate-source” could not resolve dependencies
  • Splash Screen will not display
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Xamarin Android | Layout style
  • Does Apportable support to build library binary (.a/.so)?
  • Sending cookie value via httpget but not getting the desired response
  • How to convert SOAP response with xsi values to json in WSO2esb
  • How to get current document uri in XSLT?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • JBoss External Properties Files in Classpath
  • Android - Material Design - NavigationView - How to put vertical scroll?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Jquery UI tool tip close icon
  • Switching to Release Build causes runtime error in Web Reference
  • Jetty Server not starting: Unable to establish loopback connection
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • Pass value from viewmodel to script in zk
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • QuartzCore.framework for Mono Develop
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Turn off referential integrity in Derby? is it possible?
  • JaxB to read class hierarchy
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal