Last Updated: February 25, 2016
·
8.454K
· sisardor

Resize images with ImageMagic in java

Resizing images and change image quality in Java with imagemagic

Create function called resizeImage


private static boolean resizeImage(String image_path, String quality, String size, String output_image) {
    // absolute path to ImageMagick: Command-line Tools: Convert
    String convert_path = "/usr/local/bin/convert";

    // Build process to execute convert
    ProcessBuilder pb = new ProcessBuilder(
        convert_path,
        image_path, 
        "-quality", 
        quality, 
        "-resize",
        size, 
        output_image);  

    pb.redirectErrorStream(true);  

    try {
        Process p = pb.start();
        BufferedReader br = new BufferedReader( new InputStreamReader(p.getInputStream() ));
        String line = null;  
        while((line=br.readLine())!=null){  
            System.out.println(line);  
         }
    }catch(Exception e) {  
         return false;
    }

    return true;
}

Call resizeImage() with parameters


String old_img_path = "/image_old.jpg";
String new_img_path = "/image_new.jpg";
resizeImage(old_img_path, "90%","200x", new_img_path);