【问题标题】:Hash of two PNG files from same converted PDF is different来自同一转换 PDF 的两个 PNG 文件的哈希值不同
【发布时间】:2025-12-24 22:30:11
【问题描述】:

我得到了同一个 .png 文件的不同哈希值。这些文件是使用 imagemagick convert v6.9.1-10 创建的。

文件创建:

$ convert test.pdf test_one.png
$ convert test.pdf test_two.png

Python:

import hashlib

h1 = hashlib.md5()
h1.update(open('test_one.png', 'r').read())
first_hash = h1.hexdigest()

h2 = hashlib.md5()
h2.update(open('test_two.png', 'r').read())
second_hash = h2.hexdigest()

我希望first_hashsecond_hash 相同,但事实并非如此。

为什么哈希值不一样?

【问题讨论】:

  • 您是否检查过显而易见的事情,就像磁盘上的两个文件相同,并且将第二次更新更改为读取 test_one.png 确实给出了相同的结果,因为您知道它两次执行相同的操作.第二个测试是为了确保它确实是可重复的,并且 hashlib 不会在过程中添加一些随机盐。
  • 感谢您的建议。我刚刚遇到这个答案,它为我提供了我需要的东西:*.com/questions/2654281/…。我只需要将-strip 添加到转换命令。我认为 .png 文件中必须嵌入不同的时间戳。

标签: python imagemagick hashlib


【解决方案1】:

在这里找到答案:How to remove EXIF data without recompressing the JPEG?

图像具有不同的 EXIF 数据。

convert 命令使用-strip 标志会删除所有EXIF 数据并且哈希值相同。

【讨论】:

    最近更新 更多