-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplest_god.py
79 lines (63 loc) · 2.65 KB
/
simplest_god.py
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
78
#!/usr/bin/env python3
# SGOD - Simplest JPEG Grid Origin Detector
# Copyright (c) 2019 Tina Nikoukhah <[email protected]>
# Jérémy Anger <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import io
import numpy as np
from PIL import Image
def get_jpeg_size(img, x, y):
width, height = img.size
width = (width // 8) * 8 - 8
height = (height // 8) * 8 - 8
crop_image = img.crop((x, y, width + x, height + y))
buffer = io.BytesIO()
crop_image.save(buffer, format='JPEG', quality=100)
return len(buffer.getvalue())
def detect_jpeg_grid(img, *, kappa=7, fast=False):
img = img.convert('YCbCr').split()[0]
get_size = lambda i, j: get_jpeg_size(img, i, j)
if not fast:
coords = [(i,j) for i in range(8) for j in range(8)]
result = [get_size(i, j) for i, j in coords]
sorted_result = np.sort(result)
mean = np.mean(sorted_result[16:])
std = np.std(sorted_result[16:])
mini = sorted_result[0]
else:
coords = [(i,i) for i in range(8)]
result = [get_size(i, j) for i,j in coords]
sorted_result = np.sort(result)
mean = np.mean(sorted_result[2:])
std = np.std(sorted_result[2:])
i1 = np.where(result == sorted_result[0])[0][0]
i2 = np.where(result == sorted_result[1])[0][0]
mini = np.min([sorted_result[0], get_size(i1,i2), get_size(i2,i1)])
for i in [(i1,i2), (i2,i1)]:
coords.append(i)
result.append(get_size(*i))
if mini < mean - kappa * std:
return coords[np.argmin(result)]
return -1, -1
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("input_filename", help="path to the input image")
parser.add_argument("--kappa", type=float, default=7.)
parser.add_argument("--fast", action='store_true')
args = parser.parse_args()
img = Image.open(args.input_filename)
x, y = detect_jpeg_grid(img, kappa=args.kappa, fast=args.fast)
print(x, y)