-3

I am trying to achieve something on python.

I wanna compare 2 images and see how much similar they are. I found SSIM method, but my images may have different resolution and color mode, so they cant be compared with SSIM-PIL.

My idea was converting the png image into jpg, but when I do that, all the background turns black, which results into a mess for the operations after.

I know Pillow has the method convert() for images, but if I convert a RGBA color mode image into RGB, the background goes black.

Any suggestions please?

EDIT

Please stop saying there is another question similar that may help me, because it doesnt.

Azazel
  • 183
  • 1
  • 1
  • 10
  • 1
    JPG doesn't have a transparency level (i.e., the A in RGBA), hence the background going black. How about vonverting your JPGs in PNG instead? – GPhilo Aug 08 '19 at 14:16
  • Possible duplicate of [Convert png to jpeg using Pillow in python](https://stackoverflow.com/questions/43258461/convert-png-to-jpeg-using-pillow-in-python) – Jake P Aug 08 '19 at 14:16
  • I know JPG doesnt have Alpha channel. And i though about converting my JPG into PNG. I dont know how to do it. I mean, i should remove the white pixels that are not inside my picture, because maybe his eyes are white, but i dont wanna remove them – Azazel Aug 08 '19 at 14:20
  • Simply stating that other suggested questions are not duplicates doesn't mean that they actually aren't. In fact, you have to address why the other suggested questions differ from your use case. – WiseDev Aug 08 '19 at 14:27
  • Read the other question, read mine, and you will see why they are different. – Azazel Aug 08 '19 at 14:28
  • Read the other questions, and read yours, and you will see why they are the same.. So who is right? .. :) – WiseDev Aug 08 '19 at 14:33
  • of course I am right. Since before making this one I checked the other – Azazel Aug 08 '19 at 14:36
  • I suggest you add some sample code and a link to the image you want to convert to your question. – martineau Aug 08 '19 at 14:53

2 Answers2

0

Similarly to what others have said, JPG images lack any kind of channel to facilitate transparency. Without knowing what images you have and what you're trying to accomplish with them, I'd recommend turning both of your images into PNG images, then converting them both into RGB numpy arrays, and then writing your own difference function to measure difference however you want. A simple example might be:

def pixel_difference(pixel_a, pixel_b):
    sum = 0
    for i in range(len(pixel_a)):
        sum += (pixel_a[i]-pixel_[b])**2 # Take square sum

    return sum**0.5 # Return square root

def two_image_difference(image_a, image_b):
    sum = 0
    for x in range(image_a.shape[0]):
        for y in range(image_a.shape[1]):
            sum += pixel_difference(image_a[x,y], image_b[x,y]) 

    return sum

You could also look into image cross-correlation if you need something fancier. Hope this helps!

0

Try pasting your PNG on a white background:

from PIL import Image
img = Image.open('your.png')
new_img = Image.new("RGBA", img.size, "WHITE") 
new_img.paste(img, (0, 0), img)            
new_img.convert('RGB').save('your.jpg', "JPEG")
0asa
  • 224
  • 1
  • 8