python 比较两个文件不同_如何比较两个图像文件从两个不同的文件在python

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用第三方库 `psd-tools` 来读取和写入 PSD 文件,并使用 `Image` 模块来处理图像。下面是一个简单的示例代码: ``` from psd_tools import PSDImage from PIL import Image # 打开两个 PSD 文件 psd1 = PSDImage.open('file1.psd') psd2 = PSDImage.open('file2.psd') # 获取每个 PSD 文件的图层 layers1 = psd1.layers layers2 = psd2.layers # 创建一个新的 PSD 文件,大小为两个原始文件的宽度和高度之和 new_psd = PSDImage(width=psd1.header.width + psd2.header.width, height=max(psd1.header.height, psd2.header.height)) # 将每个 PSD 文件的图层复制到新的 PSD 文件中 for i, layer in enumerate(layers1): img = layer.as_PIL() new_psd.compose(img, position=(layer.left, layer.top)) for i, layer in enumerate(layers2): img = layer.as_PIL() new_psd.compose(img, position=(psd1.header.width + layer.left, layer.top)) # 保存新的 PSD 文件 new_psd.save('merged.psd') ``` 这个代码将两个 PSD 文件的图层合并成一个新的 PSD 文件。请注意,这个代码仅合并了图层,而不是其他 PSD 文件中可能包含的其他元素(例如,样式、文本、形状等)。 ### 回答2: 合并两个PSD文件成为一个PSD文件可以通过使用Python中的PIL库(Python Imaging Library)来实现。下面是一个简单的方法来达到这个目的: 首先,您需要确保已经安装了PIL库。如果没有安装,可以使用以下命令进行安装: ``` pip install Pillow ``` 接下来,您可以使用以下代码将两个PSD文件合并成一个PSD文件: ```python from PIL import Image # 打开两个PSD文件 image1 = Image.open('file1.psd') image2 = Image.open('file2.psd') # 创建新的PSD文件 new_image = Image.new('RGBA', (max(image1.width, image2.width), max(image1.height, image2.height))) # 将第一个PSD文件复制到新的PSD文件中 new_image.paste(image1, (0, 0)) # 将第二个PSD文件复制到新的PSD文件中 new_image.paste(image2, (0, 0), mask=image2) # 保存合并后的PSD文件 new_image.save('merged.psd') ``` 在上面的代码中,首先打开要合并的两个PSD文件,然后创建一个新的空白PSD文件。然后,将第一个PSD文件复制到新的PSD文件中(位置为(0, 0))。接下来,将第二个PSD文件复制到新的PSD文件中,并使用第二个PSD文件作为蒙版,以确保图像的透明部分不会被覆盖。最后,保存合并后的PSD文件。 需要注意的是,上述代码中的文件名和路径需要根据您自己的情况进行修改。 ### 回答3: 在Python中,我们可以使用第三方库psd-tools来合并两个PSD文件成一个PSD文件。 首先,我们需要安装psd-tools库,可以通过pip命令来进行安装: ``` pip install psd-tools ``` 安装完成后,我们可以导入相应的模块和类,然后加载两个PSD文件: ```python from psd_tools import PSDImage # 加载第一个PSD文件 psd1 = PSDImage.open('file1.psd') # 加载第二个PSD文件 psd2 = PSDImage.open('file2.psd') ``` 接下来,我们可以创建一个新的PSD文件,并将两个PSD文件的图层合并到其中: ```python # 创建一个新的PSD文件 merged_psd = PSDImage() # 将第一个PSD文件的所有图层添加到新文件中 for layer in psd1: merged_psd.add(layer) # 将第二个PSD文件的所有图层添加到新文件中 for layer in psd2: merged_psd.add(layer) ``` 最后,我们可以保存合并后的PSD文件: ```python # 保存合并后的PSD文件 merged_psd.save('merged.psd') ``` 以上就是使用Python两个PSD文件合并成一个PSD文件的方法。我们使用psd-tools库加载和操作PSD文件,并将图层从两个PSD文件中提取并添加到一个新的PSD文件中,最后保存为一个新的PSD文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值