Print md5 hash of an image opened with Python's PIL

12,602

Solution 1

from PIL import Image
import hashlib

md5hash = hashlib.md5(Image.open('test.png').tobytes())
print(md5hash.hexdigest())

Solution 2

You could save the image to a io.BytesIO(), and take the md5 hash of its value:

import hashlib
import Image
import io

img = Image.open(FILENAME)
m = hashlib.md5()
with io.BytesIO() as memf:
    img.save(memf, 'PNG')
    data = memf.getvalue()
    m.update(data)
print(m.hexdigest())

This will compute the same md5 hash as if you saved the Image to a file, then read the file into a string and took the md5 hash of the string:

img.save(NEWFILE, 'PNG')
m = hashlib.md5()
data = open(NEWFILE, 'rb').read()
m.update(data)
print(m.hexdigest())

Note that if the Image was loaded from a lossy format such as JPEG, then the md5 hash you obtain might not be the same as the one you would obtain from the original file itself, not only because the above code saves the image in PNG format, but because, even if it were to re-save it as a JPEG, saving to a lossy format will produce different data.

Solution 3

PIL/Pillow has an Image method tobytes (or tostring in older versions) that will store the image pixel values in a byte string. Simply run your hash algorithm on the returned byte string.

This will be more efficient than writing a specific format such as PNG, since it's the native representation used internally.

Solution 4

Re: the comment: to ignore the exif, how about copying out the data into a new Image and md5 the string representation of that?

from PIL import Image
import md5

img = Image.open('test.png')
# assuming there is exif, if you should want it:
exif_data = img._getexif()
just_pixels = Image.new(img.mode, img.size)
just_pixels.putdata(img.getdata())

m = md5.new()
m.update(just_pixels.tostring())
Share:
12,602
ensnare
Author by

ensnare

Updated on June 05, 2022

Comments

  • ensnare
    ensnare about 2 years

    How can I open an image in PIL, then print the md5 hash of the image without saving it to a file and reading the file?

  • Torxed
    Torxed about 10 years
    with io.BytesIO() as memf: - If i'm not mistaken
  • unutbu
    unutbu about 10 years
    @Torxed: Thanks; that's nicer.
  • kylegill
    kylegill over 5 years
    Note that tostring has been removed from PIL: pillow.readthedocs.io/en/stable/_modules/PIL/…
  • Mark Ransom
    Mark Ransom over 5 years
    @kylegill thanks for the heads up, I've updated the answer.