Guest User

Untitled

a guest
Feb 19th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | None | 0 0
  1. import os
  2. from PIL import Image, ImageChops, ImageDraw, ImageOps
  3. from skimage.measure import label, regionprops
  4. import numpy as np
  5.  
  6. def get_image_difference(image1, image2):
  7.     diff = ImageChops.difference(image1, image2)
  8.     np_diff = np.array(diff)
  9.     num_nonzero = np.count_nonzero(np_diff)
  10.     total_pixels = np_diff.size
  11.     percentage_diff = (1 - (num_nonzero / total_pixels)) * 100
  12.     return percentage_diff
  13.  
  14. def compare_images_in_sequence_2(images):
  15.     count = 0
  16.     for i in range(len(images) - 1):
  17.         image1 = Image.open(images[i])
  18.         for j in range(i + 1, len(images)):
  19.             image2 = Image.open(images[j])
  20.             similarity = get_image_difference(image1, image2)
  21.             print(f"Similarity between in-sequence {images[i]} and {images[j]}: {similarity:.2f}%")
  22.  
  23.             image1 = ImageOps.grayscale(image1)
  24.             image2 = ImageOps.grayscale(image2)
  25.  
  26.             diff = ImageChops.difference(image1, image2)
  27.             binary_diff = np.array(diff) > 40
  28.  
  29.             labeled_diff = label(binary_diff)
  30.             regions = regionprops(labeled_diff)
  31.  
  32.             significant_regions = [region for region in regions if 8 <= region.area <= 1000]
  33.  
  34.             if significant_regions:
  35.                 image1_rgb = image1.convert("RGB")
  36.                 image2_rgb = image2.convert("RGB")
  37.  
  38.                 draw1 = ImageDraw.Draw(image1_rgb)
  39.                 draw2 = ImageDraw.Draw(image2_rgb)
  40.                 for region in significant_regions:
  41.                     minr, minc, maxr, maxc = region.bbox
  42.                     draw1.rectangle([minc, minr, maxc, maxr], outline='red', width=1)
  43.                     draw2.rectangle([minc, minr, maxc, maxr], outline='red', width=1)
  44.  
  45.                 combined_img = Image.new('RGB', (image1.width + image2.width, image1.height))
  46.                 combined_img.paste(image1_rgb, (0, 0))
  47.                 combined_img.paste(image2_rgb, (image1.width, 0))
  48.                 # combined_img.show(title=f'Image {i-1} and {i} combined')
  49.  
  50.                 # if not dir 'output' exists, create it
  51.                 if not os.path.exists('output'):
  52.                     os.makedirs('output')
  53.                 combined_img.save(f'output/combined_{i}_{j}_{count}.jpg')
  54.                 count = count + 1
  55.                 pass
  56.  
  57. def main(directory):
  58.     all_images = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.JPG')]
  59.     all_images.sort()
  60.  
  61.     sequences = []
  62.     current_sequence = [all_images[0]]
  63.  
  64.     for i in range(1, len(all_images)):
  65.         img1 = Image.open(all_images[i - 1])
  66.         img2 = Image.open(all_images[i])
  67.  
  68.         similarity = get_image_difference(img1, img2)
  69.  
  70.         if similarity > 10:  # Images are in the same sequence
  71.             current_sequence.append(all_images[i])
  72.             # combined_img = Image.new('RGB', (img1.width + img2.width, img1.height))
  73.             # combined_img.paste(img1, (0, 0))
  74.             # combined_img.paste(img2, (img1.width, 0))
  75.             # combined_img.show(title=f'Image {i-1} and {i} combined')
  76.             pass
  77.         else:  # Images are in different sequences
  78.             sequences.append(current_sequence)
  79.             current_sequence = [all_images[i]]
  80.  
  81.         # print(f'Difference between {all_images[i-1]} and {all_images[i]}: {similarity:.2f}%')
  82.  
  83.     sequences.append(current_sequence)  # Add the last sequence
  84.  
  85.     for seq in sequences:
  86.         if len(seq) > 1:  # Only compare if there are at least two images in the sequence
  87.             compare_images_in_sequence_2(seq)
  88.  
  89. if __name__ == '__main__':
  90.     directory = 'images'
  91.     main(directory)
  92.  
  93.  
Advertisement
Add Comment
Please, Sign In to add comment