JAVA: How to programmatically scale down all images in a directory using ImageMagick?

You can download Image Magick for free at http://www.imagemagick.org

Images are in C:\images
ImageMagick executable is in C:\ImageMagick\convert.exe

Loop through all files in folder and scale them down 25% of their original size.

Use this command to resize the image and append “s_” to the begining of the file name.

Example:
C:\ImageMagick\convert.exe C:\images\1.jpg -resize 25% C:\images\s_1.jpg

import java.io.File;
import java.io.IOException;
 
public class ImgManipulator
{
	public static void main(String[] args)
	{
		  String path = "c:\\images\\";
		  String files;
		  File folder = new File(path);
		  File[] listOfFiles = folder.listFiles();
		  for (int i = 0; i < listOfFiles.length; i++)
		  {
if (listOfFiles[i].isFile())
{
	files = listOfFiles[i].getName();
	System.out.println("Scaling"+files);
        try
	{
Process p = Runtime.getRuntime().exec(
"C:\\ImageMagick\\convert.exe C:\\images\\"+files+" -resize 25% C:\\images\\s_"+files);
        }
	catch (IOException e)
	{
		e.printStackTrace();
	}
}
		  }
	}
 
}

Leave a Reply

Your email address will not be published. Required fields are marked *