1
votes

I am trying to compare a png image with jpeg using ssim from skimage. But the similarity is less than 60 percent even though the images are the same. Is it possible to compare png and jpg images using ssim. Is there any other method to do the same?

from skimage.io import imread
from skimage.metrics import structural_similarity as ssim
from skimage.transform import resize

im1 = imread("image1.png")
im1 = resize(im1, (256, 256))
im2 = imread("image2.jpg")
im2 = resize(im2, (256, 256))
similarity = ssim(im1, im2)
print(similarity)

output:

0.5043544790072482

image1.png image1.png

image2.jpg image2.jpg

Both the images are generated by iOS testing tools.

PS: I know that the images are different when comparing at pixel level. But they look the same, so I am expecting the comparison result should also reflect the same. Is there any way to do that?

Also note that the images uploaded are for reference. Stackoverflow uses imgur in the background which automatically converts jpg to png.

1
It would be useful to show your 2 images and mention how you created them.Mark Setchell
@MarkSetchell Thanks Mark, I had updated my question.codeslord
The two images you've provided are pngs of different dimensions...Juan
@juan, I am resizing them to 256x256 images. The first one is a png image and second one is a jpg image. Maybe imgur converted it to png?codeslord

1 Answers

3
votes

Differently from PNG, JPEG is a lossy compression, which means that during compression a part of the input data (hopefully the least important) is discarded, thus introducing compression artifacts.

When you export a PNG image to JPEG, usually the program you are using lets you tune the desired amount of compression (or viceversa, the output quality): the tradeoff is between output quality and output size.


If your the structural similarity between your two images is low and you are sure that no other transformation was applied, it might be that you was too aggressive while exporting your image to JPEG. In this case, re-doing the export to JPEG keeping a better output quality should fix your issue.

For example, these are the results I get computing the structural similarity from this PNG image of Lena using the same image exported in JPEG (through GIMP) with output quality 90/100, 50/100 and 10/100 respectively.

File name         File size        SSIM (w.r.t. lena.png)
--------------------------------------------------------
lena.png          463 KB           1.0
lena_90.jpg       101 KB           0.91
lena_50.jpg        43 KB           0.85
lena_10.jpg        23 KB           0.73

Edit: in your particular case, it simply looks to me that the two images you are comparing are fairly different. So I guess your relatively low SSIM is simply due to the difference in widget size and position.

If you want to appreciate the difference you should see both next to each other (I'm using the images you just posted):

import cv2
import numpy as np

dst_size = (256, 512)
screen1 = cv2.resize(cv2.imread(r'screen1.png'), dst_size)
screen2 = cv2.resize(cv2.imread(r'screen2.png'), dst_size)

diff = abs(screen1.astype(float) - screen2.astype(float)).astype(np.uint8)

cat_image = np.concatenate([screen1, screen2, diff], axis=1)

cv2.imwrite('screeen_cat.png', cat_image)

enter image description here