How can I modify image from java through ImageMagick
? Is there any way of doing it?

- 4,350
- 9
- 52
- 78

- 271
- 2
- 4
- 6
-
1Please add a question mark (?) to questions. Please use an upper case letter at the start of every sentence. That passage was painful to read in all lower case. It pays to be more specific about what you mean by 'modify'. As to resizing (end even watermarking), this can be done using Java 2D (in the J2SE) - no need for a 3rd party API. The only caveat is the limited range of image file types offered by J2SE. (And if you wanted to deal with image types like TIFF, I would recommend JAI.) – Andrew Thompson Mar 01 '11 at 06:26
-
@Andrew Thompson: done. Yep, you can do using JAI. I have used both ImageMagick as well JAI. There are two main problem why I avoid JAI wherever I can. (1) Image quality degrades heavily, specially in heavy scaling-down. There are way to many parameters you need to know in order to get correct result with decent quality. (2) It's not easy to use. – Nishant Mar 01 '11 at 07:10
5 Answers
Use JMagick (docs). Read the documentation. It provides all the functionality of ImageMagick. You may also look into another ImageMagick Java wrapper, im4java.
There is a good starters document for im4java here
Here is an example, I've worked out.
/** Typical scaling implementation using JMagick **/
ImageInfo origInfo = new ImageInfo(absPath); //load image info
MagickImage image = new MagickImage(origInfo); //load image
image = image.scaleImage(finalWidth, finalHeight); //to Scale image
image.setFileName(absNewFilePath); //give new location
image.writeImage(origInfo); //save
Edit #1:
If you are wondering for the Jar file of JMagick. Download jMagick tarball, untar it.
$ tar xvzf jmagick-linux-6.4.0-Q32.tar.gz
./jmagick-6.4.0.jar
./jmagick.jar
./libJMagick-6.4.0.so
./libJMagick.so

- 54,584
- 13
- 112
- 127
-
@nishant thanks for ur help can u give some example where imagemagick is used with java.... – rahul the great Mar 01 '11 at 05:00
-
@rahul the great: do you want some code? The most common use is image resizing and watermarking. – Nishant Mar 01 '11 at 05:02
-
@nishant yaaa i want to resize the image .please post it thank you very much for ur help – rahul the great Mar 01 '11 at 05:16
-
@nishant im unable to find a jar file in the document u provided should i install any software or just include the jar file..please help me out .thanks in advance – rahul the great Mar 01 '11 at 05:33
-
@rahul the great :) As I said earlier, all the stuffs are available on the website mention. Is it really difficult to find downloads location on the website? http://downloads.jmagick.org/6.3.9/ – Nishant Mar 01 '11 at 05:42
-
@rahul the great: You need to install ImageMagick, ofcourse.and then put the DLL if you're working in Windows. – Nishant Mar 01 '11 at 05:54
-
-
@Nishant Thanks. Do you know how I can get the ImageInfo of a BufferedImage? – Ali Oct 29 '13 at 16:37
-
P.S.. there's no .jar file on the website which I can see, e.g http://www.jmagick.org/6.4.0/ just shows some .rpms and a .tar.gz for ImageMagick – Ali Oct 29 '13 at 16:38
-
tar for JMagick too! updated ans. Been ages since I last worked on jmagick. I'd doo what you'd. I will read the docs. Sorry. – Nishant Oct 29 '13 at 16:49
How to Install JMajick on Windows
- Go to http://downloads.jmagick.org/6.3.9/ (or any other version of your choice)
- Download ImageMagick-6.3.9-0-Q8-windows-dll.exe and jmagick-win-6.3.9-Q8.zip.
- Install the exe file. This will install ImageMagick which is a prerequisite for JMagick to work.
- Now extract the zip file. This will give jmagick.dll and jmagick.jar.
- Copy the jmagick.jar to you lib folder and include it in the classpath.
- Copy the jmagick.dll to the root installation directory of ImageMagic and add it as an entry to the PATH environment variable.
- JMagick is installed :).

- 168,117
- 40
- 217
- 433

- 91
- 1
- 1
-
2this is great ! I faced an exception (using Windows 7) "UnsatisfiedLinkError" untill I kept the 'jmagick.dll' inside my JRE_HOME/bin and needed a system-restart as well to load the dll. – tusar Jan 03 '12 at 07:47
For ImageMagic 1.4.0
// create command
ConvertCmd cmd = new ConvertCmd();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.addImage("source_picture.jpg"); // source file
op.resize(800,600);
// of op.resize(800); // and height calculate automatically
op.addImage("resized_picture.jpg"); // destination file file
// execute the operation
cmd.run(op);
And if you like maven!
<dependency>
<groupId>org.im4java</groupId>
<artifactId>im4java</artifactId>
<version>1.4.0</version>
</dependency>

- 9,008
- 4
- 43
- 54
-
-
I'm not sure about it. It used it long time ago. But I think you can simply remove resize() and change resized_picture.jpg to pdf_version.pdf – user1079877 Apr 05 '15 at 12:39
-
Facing "FileNotFound" exception for .jpg to .png conversion while using the same with comprised war file. As it's trying to get absolute path at runtime; which won't allow that to go across your zipped war file. Do you have any idea? – Krunal Shah May 08 '18 at 11:52
I tried using imagemagick from Java but found hardware accelerated 100% java library for image operations.
https://github.com/thebuzzmedia/imgscalr
"This library makes uses of efficient Java2D scaling techniques advocated by the Java2D team which provides hardware accelerated operations on most platforms."

- 481
- 7
- 6
Resizing an image (using the easiest method) within the J2SE.
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;
class ResizeImage {
public static void main(String[] args) throws Exception {
URL url = new URL("http://pscode.org/media/citymorn2.jpg");
final BufferedImage bi = ImageIO.read(url);
Runnable r = new Runnable() {
public void run() {
JLabel unresize = new JLabel(new ImageIcon(bi));
int width = (int)(bi.getWidth()*.75);
int height = (int)(bi.getHeight()*.75);
BufferedImage bi1 = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g1 = bi1.getGraphics();
g1.drawImage( bi, 0, 0, width, height, null );
JLabel easyResize = new JLabel(new ImageIcon(bi1));
JPanel p = new JPanel();
p.add( unresize );
p.add( easyResize );
JOptionPane.showMessageDialog(null, p);
}
};
SwingUtilities.invokeLater(r);
}
}

- 168,117
- 40
- 217
- 433