File size: 2,378 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Error Level Analysis (ELA) Detector

This module provides a function to perform Error Level Analysis (ELA) on images to detect potential manipulations or edits.


## Function: `run_ela`

```python
def run_ela(image: Image.Image, quality: int = 90, threshold: int = 15) -> bool:
```


### Description

Error Level Analysis (ELA) works by recompressing an image at a specified JPEG quality level and comparing it to the original image. Differences between the two images reveal areas with inconsistent compression artifacts — often indicating image manipulation.

The function computes the maximum pixel difference across all color channels and uses a threshold to determine if the image is likely edited.

### Parameters

| Parameter   | Type        | Default | Description                                                                                 |
| ----------- | ----------- | ------- | ------------------------------------------------------------------------------------------- |
| `image`     | `PIL.Image` | N/A     | Input image in RGB mode to analyze.                                                         |
| `quality`   | `int`       | 90      | JPEG compression quality used for recompression during analysis (lower = more compression). |
| `threshold` | `int`       | 15      | Pixel difference threshold to flag the image as edited.                                     |


### Returns

`bool`

* `True` if the image is likely edited (max pixel difference > threshold).  
* `False` if the image appears unedited.


### Usage Example

```python
from PIL import Image
from detectors.ela import run_ela

# Open and convert image to RGB
img = Image.open("example.jpg").convert("RGB")

# Run ELA detection
is_edited = run_ela(img, quality=90, threshold=15)

print("Image edited:", is_edited)
```


### Notes

* The input image **must** be in RGB mode for accurate analysis.  
* ELA is a heuristic technique; combining it with other detection methods increases reliability.  
* Visualizing the enhanced difference image can help identify edited regions (not returned by this function but possible to add).


### Installation

Make sure you have Pillow installed:

```bash
pip install pillow
```


### Running Locally

Just put the function in a notebook or script file and run it with your image. It works well for basic images.


### Developer

Pujan Neupane