打印用Python的PIL打开的图像的md5哈希

时间:2022-10-28 18:52:54

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?

如何在PIL中打开图像,然后打印图像的md5哈希值而不将其保存到文件并读取文件?

3 个解决方案

#1


10  

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

您可以将图像保存到io.BytesIO(),并获取其值的md5哈希值:

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:

这将计算相同的md5哈希,就像将图像保存到文件一样,然后将文件读入字符串并获取字符串的md5哈希值:

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.

请注意,如果Image是从JPEG等有损格式加载的,那么您获得的md5哈希值可能与您从原始文件本身获得的哈希值不同,这不仅仅是因为上面的代码将图像保存为PNG格式,但是因为,即使将其重新保存为JPEG,保存为有损格式也会产生不同的数据。

#2


2  

PIL has an Image method tostring that will store the image pixel values in a string. Simply run your hash algorithm on the returned string.

PIL有一个Image方法tostring,它将图像像素值存储在一个字符串中。只需在返回的字符串上运行哈希算法即可。

#3


1  

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

Re:评论:忽略exif,如何将数据复制到新的Image和md5中的字符串表示呢?

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())

#1


10  

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

您可以将图像保存到io.BytesIO(),并获取其值的md5哈希值:

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:

这将计算相同的md5哈希,就像将图像保存到文件一样,然后将文件读入字符串并获取字符串的md5哈希值:

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.

请注意,如果Image是从JPEG等有损格式加载的,那么您获得的md5哈希值可能与您从原始文件本身获得的哈希值不同,这不仅仅是因为上面的代码将图像保存为PNG格式,但是因为,即使将其重新保存为JPEG,保存为有损格式也会产生不同的数据。

#2


2  

PIL has an Image method tostring that will store the image pixel values in a string. Simply run your hash algorithm on the returned string.

PIL有一个Image方法tostring,它将图像像素值存储在一个字符串中。只需在返回的字符串上运行哈希算法即可。

#3


1  

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

Re:评论:忽略exif,如何将数据复制到新的Image和md5中的字符串表示呢?

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())