import cv2
from PIL import Image
def highlight_differences(image1_path, image2_path, output_path):
image1 = cv2.imread(image1_path)
image2 = cv2.imread(image2_path)
gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
diff = cv2.absdiff(gray1, gray2)
_, thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
cv2.rectangle(image1, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imwrite(output_path, image1)
cv2.waitKey(0)
cv2.destroyAllWindows()
add_image_to_tiff(output_path, image2_path)
def add_image_to_tiff(tiff_path, image_path):
tiff_image = Image.open(tiff_path)
new_image = Image.open(image_path)
images = [tiff_image]
try:
while True:
tiff_image.seek(tiff_image.tell() + 1)
images.append(tiff_image.copy())
except EOFError:
pass
images.append(new_image)
images[0].save(tiff_path, save_all=True, append_images=images[1:], compression="tiff_deflate")
img_1 = "IMAGE_01.tif"
img_2 = "IMAGE_02.tif"
diff_2 = "IMAGE_diff.tif"
highlight_differences(img_1, img_2, diff_2)