I have an image file, and would like to use Python to edit the image without visibly modifying the picture, while still changing the file's MD5 hash.

What's the best way to do this?

有帮助吗?

解决方案 3

I ended up using pyexiv2 to modify the image's metadata, like this:

>>> md5sum('photo.jpg')
'89dd603a0ce14750799a5144a56fbc12'
>>> image = pyexiv2.ImageMetadata('photo.jpg')
>>> image.read()
>>> image['Exif.Image.ImageDescription'] = '%030x' % random.randrange(256**15)
>>> image.write()
>>> md5sum('photo.jpg')
'426cc91835e7f4f5e92c5a48850adc05'

其他提示

import hashlib
hashlib.md5(open('image.png','rb').read()).hexdigest() # rb = readbyte ,so it will work for text as well as media (image,video) files

output >>> '724c6d87452c3a137ef1499c2d4b6576' # md5 hash value

file = open('image.png', 'rb').read()
with open('new_image.png', 'wb') as new_file:
  new_file.write(file+'\0')  #here we are adding a null to change the file content

hashlib.md5(open('new_image.png','rb').read()).hexdigest()

output >>> 'a345838e8af07b65344e19989c7c5d85' # new md5 hash value of the same media file

Use @Martijn Pieters' solution: just change one bit in the headers or somewhere safe.

Or more easy, if you may change the file size: Append a '\0' (well, any character will do) to the file. It will still be a valid JPEG file, and there will be no visible change.

echo -n ' ' >> my_image.jpeg

One crude solution is

  • to convert (one color plane of) the image into the Fourier space (using dft/fft),
  • changing some pixel values in the low frequencies (mostly stored in the bottom-right of the 2-d array),
  • and convert the image back into the image space (using ifft).

All pixels will be different with little to no optical change.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top