I am working on a Spring based application in which we would like to use ImageMagick for some image processing. We have some commands, which I want to try out after text-processing(mainly splitting the string at specific length), and then calling ImageMagick commands. Unfortunately, whenever I run the Process class, I get strange errors, but when I copy paste the exact command in my terminal, it works good.
Example code :
public @ResponseBody void setText(){
String text = "Lorem ipsum <NAME> dolor sit amet, consectetuer adipiscing elit." +
" Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus " +
"et magnis dis <NAME> parturient montes, nascetur ridiculus mus. " +
"Donec quam felis, ultricies nec, pellentesque eu, pretium quis";
String processedText = "";
if(text.length()>20){
for (int i = 0; i < text.length(); i += 20) {
//processedText += "\n";
processedText += text.substring(i, Math.min(i + 20, text.length()));
}
}
try {
String path = "convert /home/akshay/output.png -font /home/akshay/cabin.ttf -gravity west -pointsize 13 " +
"-annotate +50+300 \n'"+processedText+"' /home/akshay/output.jpg";
System.out.println("path is "+path);
Process proc = Runtime.getRuntime().exec(path);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));
System.out.println("Here is the standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.out.println("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}catch (Exception e){
e.printStackTrace();
}
}
Error log :
path is convert /home/akshay/output.png -font /home/akshay/cabin.ttf -gravity west -pointsize 13 -annotate +50+300
'Lorem ipsum <NAME> dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis <NAME> parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis' /home/akshay/output.jpg
Here is the standard output of the command:
Here is the standard error of the command (if any):
convert: unable to open image `ipsum': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `<NAME>': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `dolor': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `sit': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `amet,': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `consectetuer': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
convert: unable to open image `adipiscing': No such file or directory @ error/blob.c/OpenBlob/2712.
convert: no decode delegate for this image format `' @ error/constitute.c/ReadImage/501.
We experience this problem many times, standard commands often work weirdly, but I can't seem to find a solution for this one.
Updated command :
ProcessBuilder pb = new ProcessBuilder("convert", "/home/akshay/output.png",
"-gravity west","-pointsize 13","-annotate +50+300","'"+processedText+"'","/home/akshay/output.jpg");
Error log :
Here is the standard output of the command:
Here is the standard error of the command (if any):
convert: unrecognized option `-gravity west' @ error/convert.c/ConvertImageCommand/1722.
2nd variation tried
Process proc = Runtime.getRuntime().exec(new String[]{"convert", "/home/akshay/output.png", "-font /home/akshay/cabin.ttf",
"-gravity west","-pointsize 13","-annotate +50+300","'"+processedText+"'","/home/akshay/output.jpg"});
Error log :
Here is the standard output of the command:
Here is the standard error of the command (if any):
convert: unrecognized option `-font /home/akshay/cabin.ttf' @ error/convert.c/ConvertImageCommand/1643.