Link to home
Start Free TrialLog in
Avatar of mybluegrass
mybluegrass

asked on

imagemagick convert identical image with different name

I'm trying to use imagemagick to convert images generated on the fly with random names. The chances are that the newly generated image might be identical to previous generated image but with different file name.  For example, I got two original images which are identical (I've used java's bytearray to test):
image1.jpg
image2.jpg

When I used convert -density 100 to convert the image,
convert -density 100 image1.jpg image1.png
convert -density 100 image2.jpg image2.png

 I would expect the converted images (image1.png and image2.pgn) should be the same. But when I use java's bytearray to test, they turn out to be different.

File file = new File("c:/image1.png");
        if (file.exists()) {
        try {
          InputStream is = new FileInputStream(file);
          byte[] fileBytes1 = new byte[is.available()];
          is.read(fileBytes1);
          is.close();
         
          file = new File("c:/image2.png");
          is = new FileInputStream(file);
          byte[] fileBytes2 = new byte[is.available()];
          is.read(fileBytes2);
          is.close();
                   
          boolean blnResult = Arrays.equals(fileBytes1, fileBytes2);
          System.out.println("Are two byte arrays equal ? : " + blnResult);

How can ImageMagick convert two identical files with different filenames to the same image?

Thanks!
         
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

That's because available(), as in

>> byte[] fileBytes1 = new byte[is.available()];

doesn't guarantee to read all the file. Try
byte[] fileBytes1 = new byte[(int)file.length()];
is = new DataInputStream(new FileInputStream(file));
is.readFully(fileBytes1);
is.close();

Open in new window

Avatar of mybluegrass
mybluegrass

ASKER

Tried but still different. I don't have readFully in java 1.6:

        File file = new File("c:/image11.png");
        if (file.exists()) {
        try {
          byte[] fileBytes1 = new byte[(int) file.length()];
          InputStream is = new DataInputStream(new FileInputStream(file));
          is.read(fileBytes1);
          is.close();
         
          file = new File("c:/image2.png");
          byte[] fileBytes2 = new byte[(int) file.length()];
          is = new DataInputStream(new FileInputStream(file));
          is.read(fileBytes2);
          is.close();
                   
          boolean blnResult = Arrays.equals(fileBytes1, fileBytes2);
          System.out.println("Are two byte arrays equal ? : " + blnResult);
         
        }catch ( Exception e ) {
              e.printStackTrace();
        }
        }
Sorry - that should have been
DataInputStream is = new DataInputStream(new FileInputStream(file));

Open in new window

> That's because available(), as in
> doesn't guarantee to read all the file. Try

avaiable() doesn't actually read anything

>           is.read(fileBytes1);

see the example here

http://helpdesk.objects.com.au/java/how-do-i-read-the-contents-of-a-file-into-a-byte-array

ASKER CERTIFIED SOLUTION
Avatar of mybluegrass
mybluegrass

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This needs more looking into
If you use the following, you'll find your problem disappears probably
 	/**
	 * Fill a byte array with the contents of a file
	 *
	 * @param fileName The file to use
	 *
	 * @return The resulting byte array
	 */
	public static byte[] fileToByteArray(String fileName) {
		DataInputStream in = null;
		byte[] result = null;

		try {
			File f = new File(fileName);
			int fileSize = (int) f.length();
			in = new DataInputStream(new FileInputStream(f));
			result = new byte[fileSize];
			in.readFully(result);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (Exception e) { /* ignore */
			}
		}
		return result;
	}

Open in new window

> Answer is in this post: http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=16406

good to see you found a solution, thanks for posting