问题
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?
回答1:
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.
回答2:
from PIL import Image
import hashlib
md5hash = hashlib.md5(Image.open('test.png').tobytes())
print(md5hash.hexdigest())
回答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.
回答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())
来源:https://stackoverflow.com/questions/24126596/print-md5-hash-of-an-image-opened-with-pythons-pil