File size: 2,042 Bytes
			
			| d307493 c0b0cd4 d307493 c0b0cd4 d307493 c0b0cd4 d307493 c0b0cd4 | 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import numpy as np
import cv2 as cv
from time import time
def compress_jpg(image, quality):
    """Compress image using JPEG compression."""
    encode_param = [int(cv.IMWRITE_JPEG_QUALITY), quality]
    _, buffer = cv.imencode('.jpg', image, encode_param)
    return cv.imdecode(buffer, cv.IMREAD_COLOR)
def desaturate(image):
    """Convert image to grayscale."""
    return cv.cvtColor(image, cv.COLOR_BGR2GRAY)
def create_lut(contrast, brightness):
    """Create lookup table for contrast and brightness adjustment."""
    lut = np.arange(256, dtype=np.uint8)
    lut = cv.LUT(lut, lut)
    lut = cv.convertScaleAbs(lut, None, contrast/128, brightness)
    return lut
def elapsed_time(start):
    """Calculate elapsed time since start."""
    return f"{time() - start:.3f}s"
def genELA(img, quality=75, scale=50, contrast=20, linear=False, grayscale=False):
    """
    Perform Error Level Analysis on an image.
    
    Args:
        img: Input image (numpy array)
        quality: JPEG compression quality (1-100)
        scale: Output multiplicative gain (1-100)
        contrast: Output tonality compression (0-100)
        linear: Whether to use linear difference
        grayscale: Whether to output grayscale image
    
    Returns:
        Processed ELA image
    """
    # Convert image to float32 and normalize
    original = img.astype(np.float32) / 255
    
    # Compress image
    compressed = compress_jpg(img, quality)
    compressed = compressed.astype(np.float32) / 255
    
    # Calculate difference based on mode
    if not linear:
        difference = cv.absdiff(original, compressed)
        ela = cv.convertScaleAbs(cv.sqrt(difference) * 255, None, scale / 20)
    else:
        ela = cv.convertScaleAbs(cv.subtract(compressed, img), None, scale)
    
    # Apply contrast adjustment
    contrast_value = int(contrast / 100 * 128)
    ela = cv.LUT(ela, create_lut(contrast_value, contrast_value))
    
    # Convert to grayscale if requested
    if grayscale:
        ela = desaturate(ela)
    
    return ela | 
 
			
