문제

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