File size: 1,165 Bytes
0b8f50d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import numpy as np
from PIL import Image
from scipy.fft import fft2, fftshift


def run_fft(image: Image.Image, threshold: float = 0.92) -> bool:
    """
    Detects potential image manipulation or generation using FFT-based high-frequency analysis.

    Parameters:
        image (PIL.Image.Image): The input image.
        threshold (float): Proportion of high-frequency components above which the image is flagged.

    Returns:
        bool: True if the image is likely AI-generated or manipulated, False otherwise.
    """
    gray_image = image.convert("L")

    resized_image = gray_image.resize((512, 512))


    image_array = np.array(resized_image)

    fft_result = fft2(image_array)

    fft_shifted = fftshift(fft_result)

    magnitude_spectrum = np.abs(fft_shifted)
    max_magnitude = np.max(magnitude_spectrum)
    if max_magnitude == 0:
        return False  # Avoid division by zero if image is blank
    normalized_spectrum = magnitude_spectrum / max_magnitude

    high_freq_mask = normalized_spectrum > 0.5

    high_freq_ratio = np.sum(high_freq_mask) / normalized_spectrum.size

    is_fake = high_freq_ratio > threshold
    return is_fake