emcconville / wand

The ctypes-based simple ImageMagick binding for Python
http://docs.wand-py.org/
Other
1.4k stars 197 forks source link

inconsistent similarity values #571

Closed Appleweed564 closed 2 years ago

Appleweed564 commented 2 years ago

Not sure if this is intended but the similarity , get_image_distortion , and compare functions all return base values , when converting from a png to a jpeg.

from wand.image import Image as wand_image

allmethods = ['undefined','absolute','fuzz','mean_absolute','mean_error_per_pixel',
    'mean_squared','normalized_cross_correlation','peak_absolute','peak_signal_to_noise_ratio','perceptual_hash',
    'root_mean_square','structural_similarity','structural_dissimilarity']

with wand_image(filename=dog.png) as img:
  with img.convert('jpeg') as converted:
      converted.sampling_factors="4:2:0"
      converted.compression_quality = 20
      converted.interlace_scheme = 'jpeg'
      result_metric = img.get_image_distortion(converted)
          #location, diff = img.similarity(converted,0.05,each0)
          #result_image, result_metric = img.compare(converted)

      for f in allmethods:
          result_metric = img.get_image_distortion(converted,f)
          print(f"{each0}={result_metric}")

This is the result metric I get with similarity , get_image_distortion , and compare functions

undefined=0.9999997416524083
absolute=0.0
fuzz=0.0
mean_absolute=0.0
mean_error_per_pixel=0.0
mean_squared=0.0
normalized_cross_correlation=0.9999997416524083
peak_absolute=0.0
peak_signal_to_noise_ratio=inf
perceptual_hash=0.0
root_mean_square=0.0
structural_similarity=1.0
structural_dissimilarity=0.0

The output jpg image seems to be consistent with the compression_quality , just the comparison values are a little off.

emcconville commented 2 years ago

Try written the JPEG to disk, and then reading it back before comparing distortions. Setting converted properties will not effect the pixel cache. Only when the JPEG encoder is invoked (at time of write) will the properties applied.