如何比较两个图像文件从两个不同的文件在python [英] How to compare two imagefile from two different files in python

查看:244
本文介绍了如何比较两个图像文件从两个不同的文件在python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个比较两个图像的程序。我需要从两个不同的文件夹的图像,并比较这些图像,如果他们相同或不相同。然后我想打印出来相同或不同。
例如,文件1将具有image1,图像2和图像3等,则文件2将具有image1,image2和image3等。我需要做这个python。我如何做到这一点?有人可以帮助我吗?我是新的编程和我是新的python以及。
我试过下面的解决方案,但它没有工作。



Note 3



如果您创建一个红色的GIF和红色的PNG并对它们进行复制, p>

 #创建红色GIF 
convert -size 128x128 xc:red red.gif
#创建红色PNG
convert -size 128x128 xc:red red.png
#比较并找不到差别
convert red.png red.gif -metric ae -compare -format%[distortion]info:
0

尽管这些文件本身存在巨大差异

  ls -l red * 
-rw-r - r-- 1 mark staff 196 1 Apr 11:52 red.gif
- rw-r - r-- 1 mark staff 290 1 Apr 11:52 red.png


I would like to create a program that compares two images. I need to take images from two different folders and compare that images if they are same or not. Then I want to print out as same or different. For example file 1 will have image1 and image 2 and image 3 , etc then file 2 will have image1,image2 and image3 etc . I need to do this python. How do I do this? Can someone help me? I am new to programming and I am new to python as well. I have tried the solution as below, but it did not work. Comparing Image Files

More information.

import math, operator
from PIL import Image
import functools
def compare(file1, file2):
    image1 = Image.open(file1)
    image2 = Image.open(file2)
    h1 = image1.histogram()
    h2 = image2.histogram()
    rms = math.sqrt( functools.reduce(operator.add,
                       map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
    return rms

if __name__=='__main__':
    import sys
    file1, file2 = sys.argv[1:]
    print( compare(file1, file2))

Actually I am using this for comparing screen taken by automation and manual testing on mobile devices. The files are *.png. I manage to get this working with below code.

the above code you need top provide the image1 and image 2 on command prompt.But I want pyton to take from imagaes form files one in one location and images from other location and compare autamatiucally. If the images are same then it should print as zero as now the above code respons. If they are different then it will be no zero. The issue I am have how I could take from two files and compare one by one form scripts. Eg. File1\Image1.png ==File2\ image1.png

解决方案

Use ImageMagick, it is available for Python and included on most Linux distros. Get familiar at the commandline first, then work it up into Python.

Create two directories

mkdir directory{1..2}

Create a black square in directory1

convert -size 128x128 xc:black directory1/1.png

Create a black square with a red 10x10 rectangle in directory2

convert -size 128x128 xc:black -fill red -draw "rectangle 0,0, 9,9"  directory2/2.png

Now ask ImageMagick to tell us how many pixels are different between the two images, -metric ae is the Absolute Error.

convert directory1/1.png directory2/2.png -metric ae -compare -format "%[distortion]" info:

Output

100

Note 1

If you want to allow the images to be nearly the same, you can add -fuzz 10% which will allow each pixel to differ by up to 10% from the corresponding pixel in the other image before counting it as different. This may be more useful when comparing JPEG images which may have slightly different quality/quantisation settings, and/or anti-aliasing, both of which cause images to differ slightly.

Note 2

You can shell out from Python and run shell scripts like the above using this... link

Note 3

If you create, say a red GIF and a red PNG, and copare them, they will come up identical, like this

# Create red GIF
convert -size 128x128 xc:red red.gif
# Create red PNG
convert -size 128x128 xc:red red.png
# Compare and find no difference
convert red.png red.gif  -metric ae -compare -format "%[distortion]" info:
0

despite the fact that the files theselves differ enormously

ls -l red*
-rw-r--r--  1 mark  staff  196  1 Apr 11:52 red.gif
-rw-r--r--  1 mark  staff  290  1 Apr 11:52 red.png

这篇关于如何比较两个图像文件从两个不同的文件在python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆