I made a question on Stack Overflow and then tried to edit it so that what I entered in the entry box was the exact text shown below in the code block.
The system would not let me post it, throwing an error about my post looking like it contained improperly formatted code. By editing it in pieces, I narrowed it down so by adding an empty line between [![Difference][2]][2]
and [![Distribution][3]][3]
at the bottom I was able to post. All the images were added using the "image" button in the toolbar.
I think this may be a bug, and I didn't want to end up posting a duplicate question so I did not try it again after I got it to work.
I downloaded a test image from Wikipedia (the tree seen below) to compare `Pillow` and `OpenCV` (using `cv2`) in Python. Perceptually the two images appear the same, but their respective `md5` hashes don't match; and if I subtract the two images the result is not even close to solid black (the image shown below the original). The original image is a JPEG. If I convert it to a PNG first, the hashes match.
The last image shows the frequency distribution of how the pixel value differences.
This is the code I used:
from PIL import Image
import cv2
import sys
import md5
import numpy as np
def hashIm(im):
im = cv2.imread(im)
imP = np.array(Image.open(im))
# Convert to BGR and drop alpha channel if it exists
imP = imP[..., 2::-1]
# Make the array contiguous again
imP = np.array(imP)
cv2.imshow('cv2', im)
cv2.imshow('PIL', imP)
cv2.imshow('diff', (im-imP))
with open('dist.csv', 'w') as outfile:
diff = im-imP
for i in range(1, 256):
outfile.write('{},{}\n'.format(i, np.count_nonzero(diff==i)))
cv2.waitKey(0)
cv2.destroyAllWindows()
return md5.md5(im).hexdigest() + ' ' + md5.md5(imP).hexdigest()
if __name__ == '__main__':
print sys.argv[1] + '\t' + hashIm(sys.argv[1])
[![Original (Wikipedia "Tree" article)][1]][1]
[![Difference][2]][2]
[![Distribution][3]][3]
[1]: https://i.sstatic.net/iWUnD.jpg
[2]: https://i.sstatic.net/UNNtB.png
[3]: https://i.sstatic.net/Vk0Y8.png