Spaces:
Running
Running
| from PIL import Image, ImageChops, ImageEnhance | |
| import io | |
| def run_ela(image: Image.Image, quality: int = 90, threshold: int = 15) -> bool: | |
| """ | |
| Perform Error Level Analysis to detect image manipulation. | |
| Parameters: | |
| image (PIL.Image): Input image (should be RGB). | |
| quality (int): JPEG compression quality for ELA. | |
| threshold (int): Maximum pixel difference threshold to classify as edited. | |
| Returns: | |
| bool: True if image appears edited, False otherwise. | |
| """ | |
| # Recompress the image into JPEG format in memory | |
| buffer = io.BytesIO() | |
| image.save(buffer, format="JPEG", quality=quality) | |
| buffer.seek(0) | |
| recompressed = Image.open(buffer) | |
| # Compute the pixel-wise difference | |
| diff = ImageChops.difference(image, recompressed) | |
| extrema = diff.getextrema() | |
| max_diff = max([ex[1] for ex in extrema]) | |
| # Enhance difference image for debug (not returned) | |
| _ = ImageEnhance.Brightness(diff).enhance(10) | |
| return max_diff > threshold | |