Image Fraud Detection

The code provided is designed to perform Error Level Analysis (ELA) on an image to detect potential alterations. ELA works by introducing compression artifacts and analyzing the differences between the original and recompressed versions of the image. The process begins by importing necessary libraries, including OpenCV for image reading, Pillow for image manipulation, and Matplotlib for plotting the results.

First, the original image is loaded using the Pillow library and converted to the RGB color mode to ensure consistent color representation. The image is then saved as a JPEG file at a reduced quality level, which introduces compression artifacts. This recompressed image is reopened for further analysis. The core of the ELA involves calculating the pixel-wise difference between the original and recompressed images using the ImageChops.difference() function from Pillow. This operation highlights regions of the image that respond differently to compression, which can indicate areas of potential manipulation or simply areas with high detail that are more susceptible to compression artifacts.

ELA Result

To enhance the visibility of these differences, the difference image’s brightness is adjusted based on the maximum pixel value found in the image. This scaling ensures that the differences are more noticeable. Additionally, the image is auto-contrasted to further improve the visibility of the highlighted regions. Finally, the original image and the ELA image are displayed side by side using Matplotlib. The original image shows the content as expected, while the ELA image displays regions with higher compression differences in brighter colors against a darker background. This visualization helps in identifying areas that may have been altered or simply exhibit higher detail, necessitating further investigation for conclusive results.


    import cv2
    import numpy as np
    from PIL import Image, ImageChops, ImageEnhance, ImageOps
    import matplotlib.pyplot as plt
    import os

    def perform_ela(image_path, quality=90, scale=10):
        # Open the original image and save it as JPEG to force re-compression
        original = Image.open(image_path).convert('RGB')
        dir_name = os.path.dirname(image_path)
        temp_image_path = os.path.join(dir_name, 'temp_image.jpg')
        original.save(temp_image_path, 'JPEG', quality=quality)
        
        # Open the saved JPEG image
        recompressed = Image.open(temp_image_path)
        
        # Perform ELA by calculating the difference between the original and the recompressed images
        ela_image = ImageChops.difference(original, recompressed)
        
        # Enhance the difference to make it more visible
        extrema = ela_image.getextrema()
        max_diff = max([ex[1] for ex in extrema])
        scale_factor = 255.0 / max_diff if max_diff != 0 else 1
        ela_image = ImageEnhance.Brightness(ela_image).enhance(scale_factor)
        ela_image = ImageOps.autocontrast(ela_image)
        
        return ela_image

    # Define the image path
    image_path = './Chromosome.jpeg'

    # Perform ELA on the provided image
    ela_image = perform_ela(image_path)

    # Display the ELA result
    image = cv2.imread(image_path)
    plt.figure(figsize=(15, 7))
    plt.subplot(1, 2, 1)
    plt.title('Original Image')
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.subplot(1, 2, 2)
    plt.title('ELA Image')
    plt.imshow(ela_image, cmap='gray')
    plt.show()
    

By running the provided code on an image, you can observe the ELA result that highlights potential areas of alteration or high detail. This technique can be a valuable tool in image forensics and fraud detection, helping to identify manipulated images or regions that may require further scrutiny.

For more information on Error Level Analysis and image forensics, you can refer to the following Wikipedia links: