【问题标题】:Comparing Images for equality but output is large比较图像是否相等但输出很大
【发布时间】:2018-05-18 15:01:45
【问题描述】:

我一直在尝试以 ASCII 值读取图像并比较这两个图像以获得精确的 ASCII 值匹配。但是,输出非常大,我的硬件很旧,无法读取输出,我试图将输出保存到文件中,但文件很大。这是我想要做的:

orig = sys.stdout
f = open('output.txt','w')
sys.stdout = f

# Load the two Images 

with open("image1.jpg", "rb") as b:
 with open("image2.jpg", "rb") as a:

  # Convert the two images from binary to ascii

    chunk1 = binascii.b2a_hex(b.read())
    chunk2 = binascii.b2a_hex(a.read())

# split the two chunks of ascii values into a list of 24 bytes 

chunkSize = 24
for i in range (0,len(chunk1),chunkSize):
 for j in range (0,len(chunk2),chunkSize):

 # Print them

  list1 = chunk1[i:i+chunkSize]
  print "List1: "+ list1
  list2 = chunk2[j:j+chunkSize]
  print "List2: " + list2

# Compare the two images for equality 

  list = list1 == list2

 # print whether its a match or false

  print list

sys.stdout = orig
f.close()

# Saved to a file

它是如何工作的:

img1 具有以下十六进制:FFD8 FFE0 0010 4A46 4946 0001 0200 0064 0064 0000 FFEC 0011 img2 具有以下十六进制:FFD8 FFE0 0010 4A46 4946 0001 0210 0064 0064 0000 FFEC 0012

它将使用 img1 的前 24 个字符,并在 24 个字符中针对所有 img2 hex 进行测试,然后使用 img1 的下 24 个字符对所有 img2 hex 进行测试。示例:

List1: FFD8 FFE0 0010 4A46 4946 0001 
List2: FFD8 FFE0 0010 4A46 4946 0001 
True 

List1: FFD8 FFE0 0010 4A46 4946 0001 
List2: 0210 0064 0064 0000 FFEC 0012 
False

List1: 0200 0064 0064 0000 FFEC 0011 
List2: FFD8 FFE0 0010 4A46 4946 0001 
False 

List1: 0200 0064 0064 0000 FFEC 0011 
List2: 0210 0064 0064 0000 FFEC 0012 
False

但是,考虑到像 40k 十六进制和 20k 这样的巨大图像,输出很大,我无法从终端读取这些图像,也无法将输出保存到文件中。

如何只打印匹配的(真)24 个字符的 ASCII 十六进制值而不打印真、假和假 ASCII 十六进制值?

FFD8 FFE0 0010 4A46 4946 0001

【问题讨论】:

  • 你能只比较十六进制字节而不打印它们吗?您是否出于其他原因需要实际输出?
  • 可以直接比较chunk1 == chunk2吗?如果有区别,您是否关心它在哪里
  • 是的,我确实需要查看输出。如果您暗示直接比较 chunks1,2 而不通过循环将不会输出整个结果,对吗?如果不是,那么与比较 list1 == list2 不一样。我不关心差异,我关心的是匹配 ascii 十六进制字节输出。
  • chunk1 和 chunk2 是整个文件内容。您可以比较它们以查看文件是否相等。但这只会给你一个是/否的答案,它不会告诉你具体的字节差异是什么。
  • 没错。感谢 John 在这方面花费的时间。

标签: python printing equality


【解决方案1】:

您可以一次简单地从每个图像中读取 24 个字节,而不是一次读取整个文件。 file.read() 接受一个允许它一次读取几个字节的参数。您可以循环运行它,直到read() 返回一个空字符串,这意味着已到达文件末尾。请参阅doc

编辑

如果您只想检查两个文件是否相同,为什么不检查校验和呢?相同的文件将始终具有相同的校验和。有关更多详细信息,请参阅此answer

【讨论】:

  • 确实可以。我实际上意识到了这一点,但我想我会阅读整个图像,然后将它们分成大小均匀的字符块,比较它们并输出匹配和错误的结果,但结果变得更大并且变得不可读。你的方法要短得多。肯定会尝试一下。谢谢
【解决方案2】:

如果我理解了这个问题,那么:

orig = sys.stdout
f = open('output.txt','w')
sys.stdout = f

# Load the two Images 

with open("image1.jpg", "rb") as b:
 with open("image2.jpg", "rb") as a:

  # Convert the two images from binary to ascii

    chunk1 = binascii.b2a_hex(b.read())
    chunk2 = binascii.b2a_hex(a.read())

# split the two chunks of ascii values into a list of 24 bytes 

chunkSize = 24
for i in range (0,len(chunk1),chunkSize):
 for j in range (0,len(chunk2),chunkSize):

  list1 = chunk1[i:i+chunkSize]
  list2 = chunk2[j:j+chunkSize]

  # Compare the two images for equality 

  list = list1 == list2

  # print bytes once only if they were the same in both list1 and list2

  if list:
   print list1

sys.stdout = orig
f.close()

这将省略原始示例中为 False 的任何输出,唯一的输出将是匹配的字节。如果这不是您的意思,您能否准确说明您想要实现的目标?

【讨论】:

  • 不,你明白我的意思,对不起,如果我对这个问题有点不清楚。这正是我一直在寻找的。非常感谢。
  • 没问题,感谢您的反馈,很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 2015-03-13
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-05-31
  • 2013-08-18
  • 2016-10-20
相关资源
最近更新 更多