Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from PIL import Image, ImageChops, ImageDraw, ImageOps
- from skimage.measure import label, regionprops
- import numpy as np
- def get_image_difference(image1, image2):
- diff = ImageChops.difference(image1, image2)
- np_diff = np.array(diff)
- num_nonzero = np.count_nonzero(np_diff)
- total_pixels = np_diff.size
- percentage_diff = (1 - (num_nonzero / total_pixels)) * 100
- return percentage_diff
- def compare_images_in_sequence_2(images):
- count = 0
- for i in range(len(images) - 1):
- image1 = Image.open(images[i])
- for j in range(i + 1, len(images)):
- image2 = Image.open(images[j])
- similarity = get_image_difference(image1, image2)
- print(f"Similarity between in-sequence {images[i]} and {images[j]}: {similarity:.2f}%")
- image1 = ImageOps.grayscale(image1)
- image2 = ImageOps.grayscale(image2)
- diff = ImageChops.difference(image1, image2)
- binary_diff = np.array(diff) > 40
- labeled_diff = label(binary_diff)
- regions = regionprops(labeled_diff)
- significant_regions = [region for region in regions if 8 <= region.area <= 1000]
- if significant_regions:
- image1_rgb = image1.convert("RGB")
- image2_rgb = image2.convert("RGB")
- draw1 = ImageDraw.Draw(image1_rgb)
- draw2 = ImageDraw.Draw(image2_rgb)
- for region in significant_regions:
- minr, minc, maxr, maxc = region.bbox
- draw1.rectangle([minc, minr, maxc, maxr], outline='red', width=1)
- draw2.rectangle([minc, minr, maxc, maxr], outline='red', width=1)
- combined_img = Image.new('RGB', (image1.width + image2.width, image1.height))
- combined_img.paste(image1_rgb, (0, 0))
- combined_img.paste(image2_rgb, (image1.width, 0))
- # combined_img.show(title=f'Image {i-1} and {i} combined')
- # if not dir 'output' exists, create it
- if not os.path.exists('output'):
- os.makedirs('output')
- combined_img.save(f'output/combined_{i}_{j}_{count}.jpg')
- count = count + 1
- pass
- def main(directory):
- all_images = [os.path.join(directory, f) for f in os.listdir(directory) if f.endswith('.JPG')]
- all_images.sort()
- sequences = []
- current_sequence = [all_images[0]]
- for i in range(1, len(all_images)):
- img1 = Image.open(all_images[i - 1])
- img2 = Image.open(all_images[i])
- similarity = get_image_difference(img1, img2)
- if similarity > 10: # Images are in the same sequence
- current_sequence.append(all_images[i])
- # combined_img = Image.new('RGB', (img1.width + img2.width, img1.height))
- # combined_img.paste(img1, (0, 0))
- # combined_img.paste(img2, (img1.width, 0))
- # combined_img.show(title=f'Image {i-1} and {i} combined')
- pass
- else: # Images are in different sequences
- sequences.append(current_sequence)
- current_sequence = [all_images[i]]
- # print(f'Difference between {all_images[i-1]} and {all_images[i]}: {similarity:.2f}%')
- sequences.append(current_sequence) # Add the last sequence
- for seq in sequences:
- if len(seq) > 1: # Only compare if there are at least two images in the sequence
- compare_images_in_sequence_2(seq)
- if __name__ == '__main__':
- directory = 'images'
- main(directory)
Advertisement
Add Comment
Please, Sign In to add comment