These are my images, I want to
- Convert the PDF documents (
file_1.pdf
andfile_2.pdf
) into images. - Compare the images to detect any differences between them.
- Highlight the changes or areas of discrepancy between the two images.
This is my code
from pdf2image import convert_from_pathimport cv2import numpy as npfrom PIL import Image, ImageOpsfrom IPython.display import display`def process_and_display_image(pdf_path, target_size=(800, 600),save_path='processed_image.jpeg'): images = convert_from_path(pdf_path) image = images[0] image = ImageOps.exif_transpose(image) image.thumbnail(target_size, Image.Resampling.LANCZOS) image_np = np.array(image) image_np = cv2.cvtColor(image_np, cv2.COLOR_RGB2GRAY) image_processed = Image.fromarray(image_np) display(image_processed) image_processed.save(save_path, 'JPEG') print(f"Image saved as {save_path}")# Display image from PDFprocess_and_display_image("file_1.pdf",save_path='file_1.jpeg')process_and_display_image("file_2.pdf",save_path='file_2.jpeg')import matplotlib.pyplot as pltimage1 = cv2.imread('file_1.jpeg', cv2.IMREAD_UNCHANGED)image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2RGB)image2 = cv2.imread('file_2.jpeg', cv2.IMREAD_UNCHANGED)image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)if image1.shape == image2.shape: difference = cv2.absdiff(image1,image2)difference = 255 - differenceif image1.shape == image2.shape: overlay = cv2.addWeighted(image1, 0.5, image2, 0.5, 0) difference = overlayplt.imshow(difference)plt.axis('off')plt.show()