Image magick java
Asked Answered
U

5

27

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

Unearthly answered 1/3, 2011 at 4:37 Comment(2)
Please 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.)Maxon
@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.Snavely
S
24

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
Snavely answered 1/3, 2011 at 4:53 Comment(10)
@nishant thanks for ur help can u give some example where imagemagick is used with java....Unearthly
@rahul the great: do you want some code? The most common use is image resizing and watermarking.Snavely
@nishant yaaa i want to resize the image .please post it thank you very much for ur helpUnearthly
@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 advanceUnearthly
@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? downloads.jmagick.org/6.3.9Snavely
@rahul the great: You need to install ImageMagick, ofcourse.and then put the DLL if you're working in Windows.Snavely
@ClickUpvote updated. Looks like they removed the index file.Snavely
@Snavely Thanks. Do you know how I can get the ImageInfo of a BufferedImage?Spiritism
P.S.. there's no .jar file on the website which I can see, e.g jmagick.org/6.4.0 just shows some .rpms and a .tar.gz for ImageMagickSpiritism
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.Snavely
D
9

How to Install JMajick on Windows

  1. Go to http://downloads.jmagick.org/6.3.9/ (or any other version of your choice)
  2. Download ImageMagick-6.3.9-0-Q8-windows-dll.exe and jmagick-win-6.3.9-Q8.zip.
  3. Install the exe file. This will install ImageMagick which is a prerequisite for JMagick to work.
  4. Now extract the zip file. This will give jmagick.dll and jmagick.jar.
  5. Copy the jmagick.jar to you lib folder and include it in the classpath.
  6. Copy the jmagick.dll to the root installation directory of ImageMagic and add it as an entry to the PATH environment variable.
  7. JMagick is installed :).
Dar answered 28/9, 2011 at 16:15 Comment(1)
this 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.Fornix
S
5

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>
Suburbanize answered 11/11, 2013 at 3:25 Comment(3)
how would one mimic convert my_image.jpg pdf_version.pdf ?Antelope
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.pdfSuburbanize
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?Kinnon
M
0

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);
    }
}
Maxon answered 1/3, 2011 at 6:56 Comment(1)
Now how would you e.g store this resized image to disk?Spiritism
F
0

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."

Finlay answered 10/2, 2014 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.