Simple hash of PIL image

前端 未结 2 1206
醉话见心
醉话见心 2020-12-31 21:41

Background

I want to store information of PIL images in a key-value store. For that, I hash the image and use the hash as a key.

What I tried

I h

2条回答
  •  隐瞒了意图╮
    2020-12-31 21:58

    Recognising what you say about timestamps, ImageMagick has exactly such a feature. First, an example.

    Here I create two images with identical pixels but a timestamp at least 1 second different:

    convert -size 600x100 gradient:magenta-cyan 1.png
    sleep 2
    convert -size 600x100 gradient:magenta-cyan 2.png
    

    If I checksum them on macOS, it tells me they are different because of the embedded timestamp:

    md5 -r [12].png
    
    c7454aa225e3e368abeb5290b1d7a080 1.png
    66cb4de0b315505de528fb338779d983 2.png
    

    But if I checksum just the pixels with ImageMagick, (where %# is the pixel-wise checksum), it knows the pixels are identical and I get:

    identify -format '%# - %f\n' 1.png 2.png
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 1.png
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 2.png
    

    And, in fact, if I make a TIFF file with the same image contents, whether with Motorola or Intel byte order, or a NetPBM PPM file:

    convert -size 600x100 gradient:magenta-cyan -define tiff:endian=msb 3motorola.tif
    convert -size 600x100 gradient:magenta-cyan -define tiff:endian=lsb 3intel.tif
    convert -size 600x100 gradient:magenta-cyan 3.ppm
    

    ImageMagick knows they are the same, despite different file format, CPU architecture and timestamp,:

    identify -format '%# - %f\n' 1.png 3.ppm 3{motorola,intel}.tif
    
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 1.png
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 3.ppm
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 3motorola.tif
    70680e2827ad671f3732c0e1c2e1d33acb957bc0d9e3a43094783b4049225ea5 - 3intel.tif
    

    So, in answer to your question, I am suggesting you shell out to ImageMagick with the Python subprocess module and use ImageMagick.

提交回复
热议问题