Page 1 of 1

convert identical image with different name

Posted: 2010-06-11T12:46:25-07:00
by my600080
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. If that's the case, I don't want to write the converted image to the database. I used the MessageDigest's update to generate hash id for each unique image based on their byte array value.

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.png) should be the same. But when I use java's bytearray to test, they turn out to be different.

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

Thanks!

Re: convert identical image with different name

Posted: 2010-06-11T13:19:08-07:00
by snibgo
I think you will find the images are identical.

Code: Select all

compare -metric RMSE image1.png image2.png NULL:
But PNG files files contain other data, such as the time of the last image modification. Byte-comparison programs will pick these up.

Re: convert identical image with different name

Posted: 2010-06-11T13:30:40-07:00
by my600080
Yes, you are right. When I use "compare -metric RMSE image1.png image2.png NULL:" to compare, the result is 0 (0).

But my problem is that, the converted images are already in byte array format in the database. I am not able to loop through all the images and find if there is one that's the same as the newly converted image by using compare command.

Is there a way to truncate the "other data" (such as the modification date) from the converted png image and only use the portion that represents the image itself to generate the hash id using MessageDigest.

Thanks a lot!

Re: convert identical image with different name

Posted: 2010-06-11T13:55:28-07:00
by snibgo
Not as far as I know, for PNG. But you could use a format that records nothing but the image, such as RGB (or RGBA if you have transparency):

convert image1.png image1.rgb

Jpeg also seems to give identical files for identical input images, but RGB may be safer.

Re: convert identical image with different name

Posted: 2010-06-11T15:13:41-07:00
by my600080
I found this post that someone seems to have similar problem: viewtopic.php?f=3&t=12230

User magick mentioned to do the following the delete file property:

convert logo.jpg +set modify-date +set create-date logo.png

I am quite new to imagemagick so I'm not sure if that's what I need do. I did try it but doesn't seem to produce the same image.

He also mentioned using DeleteImageProperty(image,"modify-date") to delete the image property but since I'm calling convert from command line it doesn't work.

Thanks a lot!

Re: convert identical image with different name

Posted: 2010-06-11T16:06:06-07:00
by snibgo
I haven't come across "+set modify-date" or "+set create-date". When I try:

convert logo: +set modify-date +set create-date logo.png
identify -verbose logo.png

this still gives a creation and modification date. If this is a feature that no longer works, you might raise it in the bugs forum (referencing this thread, and the one you mention above).

Re: convert identical image with different name

Posted: 2010-06-12T07:04:05-07:00
by Drarakel
Never used this before myself.. But I think this should work:
convert image.jpg +set date:create +set date:modify image.png

The default of storing these dates in tEXt chunks when saving to PNG indeed doesn't seem to be very useful.

Re: convert identical image with different name

Posted: 2010-06-12T07:35:36-07:00
by snibgo
Good discovery, Drarakel.

On identical inputs,

convert logo1.jpg +set date:create +set date:modify logo1.png
convert logo2.jpg +set date:create +set date:modify logo2.png

gives me bytewise identical PNG files.

How did you discover this?

Re: convert identical image with different name

Posted: 2010-06-12T08:48:28-07:00
by Drarakel
Well, I think it's possible to set/delete all the extra attributes - if one is using the correct names. And the IM attributes (and also the PNG chunks) seem to be called "date:create" and "date:modify":

Code: Select all

  Properties:
    date:create: 2010-06-12T17:32:52+02:00
    date:modify: 2010-06-12T17:32:52+02:00
...
Maybe it was "modify-date"/"create-date" in some past versions?

Re: convert identical image with different name

Posted: 2010-06-14T04:53:23-07:00
by my600080
This is wonderful! It works very well for the png images conversion. Now, I am able not to generate a new png image every time even though it exists in my database. Thanks a lot for all of your help.

Re: convert identical image with different name

Posted: 2010-06-15T00:26:53-07:00
by anthony
I have added this to PNG Writing options in IM Examples....
http://www.imagemagick.org/Usage/formats/#png_write
+set date:create +set date:modify
Normally if you write the same image separately to two PNG files the two files will not compare to be the same. That is because IM will also store an image creation date within the file. JPEG images do not have this problem, but they are lossy. For example...

Code: Select all

      convert logo: logo.jpg
      convert logo.jpg logo1.png

      sleep 2; touch logo.jpg
      convert logo.jpg logo2.png

      diff -s logo1.png logo2.png
      compare -metric RMSE logo1.png logo2.png null:
The "diff' in the above will return the message
"Binary files logo1.png and logo2.png differ"
Even though the "compare" returned "0 (0)" which says the images have exactly the same image data.

The solution is to save PNG images without any 'time stamps'.
For example...

Code: Select all

      convert logo: logo.jpg
      convert logo.jpg +set date:create +set date:modify logo1.png

      sleep 2; touch logo.jpg
      convert logo.jpg +set date:create +set date:modify logo2.png

      diff -s logo1.png logo2.png
This time "diff" reported...
"Files logo1.png and logo2.png are identical"
ASIDE: you can also use the programs "cmp" or "md5sum" or "sha1sum" to compare binary image files.