ImageMagick's 'compare' seems to provide irrelevant numbers when using SSIM as the comparison metric. I'm using ImageMagick 7.0.8-58 Q16.
I tried it with various levels of WebP compression (including lossless)
magick compare -metric SSIM original.png lossless.webp difference.png
provides 0.734809. While I would expect something around 0.999 for a visually indistinguishable image (apparently no artifacts, because it's a WebP file compressed in lossless mode)
Receiving such results, I assumed that there is a problem with comparing PNG to WebP specifically, so I tried to compare original PNG to compressed JPEG with a target SSIM. SSIM package from Fred's ImageMagick Scripts gave results of ssim=0.949 and dssim=0.051. ImageMagick on contrary gave 0.711049 result for the very same file.
magick compare -metric SSIM original.png compressed.jpg difference.png
Sadly, Fred's SSIM doesn't seem to work with WebP and GraphicsMagick's compare doesn't support SSIM. So how do I get an accurate SSIM from ImageMagick or what are the other ways to get SSIM for WebP files?
Do you have webp installed as a delegate to ImageMagick? If so, it should show in the delegates list from magick -version. What is your platform/OS?
My ssim script seems to work for me on IM 7.0.8.59 Q16 Mac OSX
magick rose: rose.webp
ssim rose.webp rose.webp
ssim=1 dssim=0
Also ImageMagick compare works fine for me also.
magick compare -metric ssim rose.webp rose.webp null:
1
I believe that lower values for webp to png may have to do with the webp lossy compression. I get similar values from ImageMagick and from my script:
magick rose: rose.webp
magick rose: rose.png
magick compare -metric ssim rose.webp rose.png null:
0.895189
ssim rose.webp rose.png
ssim=0.895 dssim=0.105
However, if I use lossless compression, then I get a perfect comparision:
magick rose: -define webp:lossless=true rose.webp
magick rose: rose.png
ssim rose.webp rose.png
ssim=1 dssim=0
magick compare -metric ssim rose.webp rose.png null:
1
ADDITION:
For me, it does not matter which order the webp image is in the command line.
Here, I use lossless compression.
magick rose: rose.png
magick rose: -define webp:lossless=true rose.webp
magick compare -metric ssim rose.webp rose.png null:
1
1magick compare -metric ssim rose.png rose.webp null:
1
Here, I do not use lossless compression.
magick rose: rose.png
magick rose: rose.webp
magick compare -metric ssim rose.webp rose.png null:
0.895189
magick compare -metric ssim rose.png rose.webp null:
0.895189
Actually the answer is pretty simple. I used a bugged version of ImageMagick. Just changing the order of comparison did the trick:
magick compare -metric SSIM compressed.jpg original.png difference.png
Related
i've seen the answer of Mark Setchell (https://stackoverflow.com/users/2836621/mark-setchell), which seems to be outdated and not working anymore. How to search an image for subimages using linux console?
https://stackoverflow.com/a/31874879/17686212
How to search an image for subimages using linux console?
downloading the needle and creating the haystack image and "hiding" the needles within it works fine.
Needle Image
convert -size 256x256 gradient:lime-blue haystack.png
convert haystack.png needle.png -geometry +30+5 -composite haystack.png
convert haystack.png needle.png -geometry +100+150 -composite haystack.png
Running the specified compare Images command causes an error:
compare -metric RMSE -subimage-search haystack.png needle.png locations.png
compare: images too dissimilar `haystack.png' # error/compare.c/CompareImagesCommand/1166.
i've tried to apply -similarity-threshold and -dissimilarity-threshold and changed/lowered the convert location threshold but had no success aka bad results like this:
compare -similarity-threshold 0 -dissimilarity-threshold 1 -metric RMSE -subimage-search haystack.png needle.png locations.png
convert locations-1.png -threshold 95% txt: | grep white
locations-0.png
locations-1.png
I've used imagemagick version 7.1.0.14 on gentoo and 6.9.11.60+dfsg-1.3 on kali Linux
Hopefully you can help me to resolve this issue.
Here is how to do masked composite in ImageMagick 7.
Template:
Image:
Create Mask:
magick needle.png -transparent white -alpha extract mask.png
magick compare -metric rmse -subimage-search -dissimilarity-threshold 1 haystack.png \( -read-mask mask.png needle.png \) compare.png
9031.97 (0.137819) # 100,150
Compare-0 (differences):
Compare-1 (match scores):
If you want to find the best matches, you would need to use my script, maxima
maxima -t 80 -n 10 compare-1.png
max=1 100,150 gray=56540,220,86.2745%
max=2 30,5 gray=54484,212,83.137%
You will not get a good result from ImageMagick 6 with
compare -metric rmse -subimage-search -dissimilarity-threshold 1 haystack.png needle.png compare.png
26789 (0.408773) # 96,114
because there is too much white in your needle image and it will mismatch with the green and blue in the haystack image. Unfortunately, you cannot do masked compare in ImageMagick 6.
I'm using ImageMagick to compare files and I want it to return exit code 0. if the images are within some threshold of similarity. However, using metric RMSE and setting dissimilarity-threshold to allow some range of variability, it still returns 1. It only seems to return 0 when I give it 2 identical images.
For example:
> imageMagick compare -verbose -metric RMSE -dissimilarity-threshold 0.5 new_file.png old_file.png null
> echo $?
new_file.png PNG 1233x835 1233x835+0+0 8-bit sRGB 325677B 0.040u 0:00.040
old_file.png PNG 1233x835 1233x835+0+0 8-bit sRGB 325712B 0.040u 0:00.039
Image: new_file.png
Channel distortion: RMSE
red: 0 (0)
green: 0.358198 (5.46575e-06)
blue: 0.438701 (6.69415e-06)
alpha: 0 (0)
all: 0.283181 (4.32106e-06)
new_file.png=>null PNG 1233x835 1233x835+0+0 8-bit sRGB 216246B 0.210u 0:00.220
1
Since these two image files have such a small amount of difference and the total score calculated (0.283181) is less than my threshold of 0.5, I'd expect these two images to register as similar and return 0. (I've experimented with numerous dissimilarity-thresholds between 0.1 and up in the millions, but they also seem to have no effect.) Am I misunderstanding how to use this argument?
Edit: I know I can get the results I want using other combinations, like using -metric AE and -fuzz 0.5%, but I'm still curious, if I can use dissimilarity-threshold with RMSE.
In Imagemagick, -metric rmse returns 0 (0) for perfectly matching images. The first value in in the quantum range of the ImageMagick compile. The second number in parenthesis is in the range 0 to 1. So, it will return values of quantum range and (1) for totally mismatched images. The dissimilarity-threshold ranges from 0 to 1. Use 1 if you want to test dissimilar images and do not want it to complain that the images are too dissimilar. It is likely you won't need -dissimilarity-metric if you are testing two same sized images, but will need it if using -subimage-search.
RMSE is a measure of difference. So if the images are the same then the difference will be 0.
For example:
convert -size 100x100 xc:white white.png
convert -size 100x100 xc:gray gray.png
convert -size 100x100 xc:black black.png
echo $?
1
compare -metric rmse white.png white.png -format "\n" null:
0 (0)
echo $?
0
compare -metric rmse white.png gray.png -format "\n" null:
compare -metric rmse white.png black.png -format "\n" null:
65535 (1)
compare -metric rmse -dissimilarity-threshold 1 white.png black.png -format "\n" null:
65535 (1)
echo $?
1
compare -metric rmse -dissimilarity-threshold 0 white.png black.png -format "\n" null:
65535 (1)
echo $?
1
So for two same sized images, -dissimilarity-threshold is irrelevant.
Your command
echo $?
is returning whether the command finished successfully or not. It is not the value of the rmse metric.
convert -size 200x200 xc:white white.png
convert -size 100x100 xc:black black.png
compare -metric rmse -subimage-search white.png black.png -format "\n" null:
compare: images too dissimilar `white.png' # error/compare.c/CompareImageCommand/1148.
echo $?
2
compare -metric rmse -subimage-search -dissimilarity-threshold 1 white.png black.png -format "\n" null:
65535 (1) # 0,0
echo $?
1
So the return code seems to be giving 0 for a perfect match, 1 for a non-perfect match and 2 for an error.
I want to watermark an image, so I used compose multiply, but for some reason it doesn't work as expected.
The command:
magick image.jpg over.png -compose multiply -resize 2048x2048 -gravity center -quality 65 -strip -composite out.jpg
The over.png get inverted first and then applied??
If use the same command with and older version of Imagemagick (x32 6.7.6-1 2012-03-17 Q16) I get the expected results.
This was tested with x64 ImageMagick 7.0.5 Q16 under Windows 10.
Ah it seems I can't post all the images.
After a lot of research, turns out it's an artifact of one of the images being a JPEG in CMYK colorspace.
Very weird, but there you go.
Adding '-profile sRGB.icc' to the line should take care of it.
FYI your syntax is not proper. You have separated -compose multiply and -composite with -resize. You should do it this way with nothing between them.
magick image.jpg over.png -gravity center -compose multiply -composite -resize 2048x2048 -strip -quality 65 out.jpg
As you said there will be issues if your input JPG is CMYK and your png will always be sRGB. So you do need to convert the CMYK to sRGB before processing.
I was able to convert my EXR image to a PNG using the techniques outlined in Image conversion from IFF and EXR formats to JPEG format .
convert 0007.exr /tmp/0007.png
Unfortunately the PNG looks quite dim.
What should I add to the imagemagick convert command line to increase the brightness?
Starting with this:
You could try -auto-gamma:
convert start.jpg -auto-gamma result.jpg
If the -auto-gamma overcooks the image for your liking, you could apply a percentage of it. So, here I clone the original image and apply auto-gamma to the clone but then only blend 80% back into the original because I feel auto-gamma overdoes it:
convert start.jpg \( +clone -auto-gamma \) \
-define compose:args=80 -compose blend -composite result.jpg
Or, another option, you could experiment with your particular images and maybe try using -modulate for the brightness, where 100% means "do nothing", so numbers over 100 increase the brightness:
convert start.jpg -define modulate:colorspace=LCHuv -modulate 160 result.jpg
You can try -auto-level, which will take the minimal value and the maximal value of your picture and then stretches the values to the full range of values:
convert input.exr -auto-level output.jpg
Note that if you picture was too bright and this does not help, then it might be that your image is stored with 32 Bit, while ImageMagick is working with 16 Bit and no HDRI support. 32 Bit input is supported if convert --version
either show Q32 as part of the version string or lists HDRI under features.
Depending on your operating system you might be able to install another variant of ImageMagick. For example, for Debian Buster we can use sudo apt list imagemagick* to see that the package imagemagick-6.q16hdri is available. Installing this package provides convert-im6.q16hdri, which allows reading 32 Bit EXR images.
EXR is in linear RGB colorspace. You want to convert it to non-linear sRGB colorspace in Imagemagick as:
Input:
convert image.exr -set colorspace RGB -colorspace sRGB output.png
I tried to use apt-get install imagemagick command to install ImageMagick on my Debian Wheezy. But when I try to diff images, I get following error:
root#work:/home/tests/YAML_SHOTS/en-us# convert 1.png 2.png -metric RMSE -compare 3.png
convert.im6: unrecognized option `-metric' # error/convert.c/ConvertImageCommand/2060.
Secondly, I tried to install ImageMagick from binary source (described here: http://www.imagemagick.org/script/install-source.php#unix). But it does not install the convert executable command.
How can I fix that?
P.S. If I remove -metric option, I get one more error:
convert.im6: unrecognized option `-compare' # error/convert.c/ConvertImageCommand/1107.
Use the compare utility directly.
compare 1.png 2.png -metric RMSE 3.png
But if you want to generate a image diff without sending the metrics to STDERR, define the -metric and -compare before the image stack.
convert -metric RMSE -compare 1.png 2.png 3.png
Assuming your ImageMagick version is a fairly recent one, try this command:
compare -metric phash 1.png 2.png delta.png
7.61662
The returned pHash value of 7.61662 indicates, that there was indeed some difference in the compared image, and that the delta.png will show some red highlighted pixels.
The red pixels indicate there is a difference in the color values of the respective pixels in the two compared images. White pixels indicate identical color values. The grayed-out, light color background of the delta.png is derived from the first image, in order to help identify better the differences in more complex images. If you do not want the background, run this modified command:
compare -metric phash 1.png 2.png -compose src delta.png
Above illustration depicts 1.png (left), 2.png (center) and delta.png (right).
Compare this to
compare -metric phash 1.png 1.png delta2.png
0
Here there is no difference, the pHash value is 0, and the delta2.png doesn't show any red pixels:
Above illustration depicts 1.png (left), 1.png (center) and delta2.png (right).
By default, the compare command will run with 72 PPI. If you need a higher resolution (for example, when comparing PDF pages), add -density 300 as the first parameter to get 300 PPI.