pythonimagemd5md5-file

How can I change an image's MD5 hash in Python?


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?


Solution

  • 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'