Question: read an image ev and then save it as ev_cp. Finally, read the saved image ev_cp. Intuitively, ev_cp should have the same value as ev pixel by pixel, but the blew code does not think so. It indicates that the saved ev_cp image has some values(pixels) different from ev.
ev = cv2.imread(image_path)
im_p = results_p+'1.jpg'
if not os.path.exists(results_p):
os.makedirs(results_p)
cv2.imwrite(im_p, ev)
#Convert 2 images to numpy arrays and compare pixel by pixel
ev_cp = cv2.imread(im_p)
flag = (ev == ev_cp).all()
print(flag)
output: False
Reason:
Be careful when using JPEG
as intermediate storage for image processing tasks since it is a lossy format and values may differ when you subsequently read them back.
Consider maybe using lossless PNG
format for intermediate storage. Or use NetPBM PGM
(greyscale) or PPM
(colour) format for a particularly simple format to read and write - though be aware it cannot persist metadata, such as copyrights or EXIF data.
Change jpg tp png could fix this issue.
im_p = results_p+str(i+1)+'.png'