Reputation: 494
M primary goal is to take in a series of .eps files and convert them to .jpg using ImageMagick and GhostScript. I have both ImageMagick and GhostScript installed in a Windows environment. I am referencing ImageMagick's convert command using Process in java with no luck. Using Window's cmd
tool, I successfully converted an EPS to JPEG by navigating to C:\Program Files\ImageMagick-6.8.9-Q16 and using the following command:
convert Raw\R_GiftcardSizeNew3x5.eps Converted\R_GiftcardSizeNew3x5.jpg
In Java, I use almost the exact same command in the following code:
public void convertEPStoJPG()
{ //commands
ArrayList<String> cmds = new ArrayList<String>();
//absolute file paths of eps files retrieved using a helper method
ArrayList<String> filePaths = this.getFilePaths();
//beginning cmd line calls
cmds.add("cmd.exe");
cmds.add("/c");
cmds.add("cd C:\\Program Files\\ImageMagick-6.8.9-Q16\\");
for (int i = 0; i < filePaths.size(); i++)
{
//conversion calls
String tempPath = filePaths.get(i);
//shortening path name
tempPath = tempPath.substring(tempPath.lastIndexOf("\\") + 1, tempPath.length());
//adding command of "convert Raw\\image.eps Converted\\image.jpg"
cmds.add("convert \\Naked Wines\\Raw\\" + tempPath + " \\Naked Wines\\Converted\\" +
tempPath.substring(0,tempPath.length() - 3) + "jpg");
}
//building process with commands
ProcessBuilder pb = new ProcessBuilder(cmds);
Process process;
try {
pb.redirectErrorStream(true);
//executing commands
process = pb.start();
BufferedReader r = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
//print output from command execution
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
}
where my files im trying to grab are C:\Program Files\ImageMagick-6.8.9-Q16\Naked Wines\Raw and the destination I am converting to is C:\Program Files\ImageMagick-6.8.9-Q16\Naked Wines\Converted.
I get an error stating "The system cannot find the path specified". Looking at previously answered questions such as How to override Windows' convert command by ImageMagick's one?, people suggest you have to override Windows convert
command. Would this be the cause of error, or is there something I am missing? I'm fairly new to ImageMagick and might have missed or misunderstood something.
Upvotes: 0
Views: 675
Reputation: 494
I ended up approaching this problem in a different way using Im4Java, a pure-java interface to the ImageMagick commandline. I installed the libraries via http://im4java.sourceforge.net/#download. Here is my code for converting eps to jpg:
public void convertESPtoJPG()
{
//initialize ImageMagick operation
IMOperation op = new IMOperation();
//setting my path allows us to use ImageMagicks "convert" vs. Windows "convert"
String myPath="C:\\Program Files\\ImageMagick-6.8.9-Q16";
ProcessStarter.setGlobalSearchPath(myPath);
op.addImage(); //in
op.addImage(); //out
ConvertCmd cmd = new ConvertCmd();
//filter out files for eps files, and load the files using included FilenameLoader
ExtensionFilter filter = new ExtensionFilter("eps");
FilenameLoader loader = new FilenameLoader(filter);
List<String> files = loader.loadFilenames("C:\\Program Files\\ImageMagick-6.8.9-
Q16\\NakedWines\\Raw\\");
//what we plan on converting our eps files to
FilenamePatternResolver resolver = new FilenamePatternResolver("%P/%f.jpg");
//iterate through loaded files
for (String img: files)
{
try {
//execute our convert commands
cmd.run(op,img,resolver.createName(img));
} catch (Exception e) {
e.printStackTrace();
}
}
}
I found this method to be much easier to understand and more forward as well.
Upvotes: 1