-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosaicimage.py
101 lines (85 loc) · 3.29 KB
/
mosaicimage.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import hashlib
import json
from contextlib import contextmanager
from os import makedirs, path
from PIL import Image
from cache import CACHE_DIR
def hash_file(fpath):
with open(fpath, "rb") as f:
return hashlib.md5(f.read()).hexdigest()
def calculate_ratio(image):
return image.size[0] / float(image.size[1])
def calculate_average_color(image):
return image.resize((1, 1), Image.Resampling.LANCZOS).getpixel((0, 0))
def calculate_orientation(image):
orientations = {1: 0, 3: 180, 6: 270, 8: 90}
try:
exif_orientation = image._getexif().get(274)
except KeyError:
exif_orientation = 1
return orientations.get(exif_orientation, 0)
class MosaicImage(object):
def __init__(self, image_path):
self.path = image_path
self.hash = hash_file(self.path)
thumbnails_dir = path.join(CACHE_DIR, "images", self.hash)
fpath = path.join(thumbnails_dir, "data.json")
try:
with open(fpath, "r") as f:
data = json.load(f)
self.average_color = data["average_color"]
self.ratio = data["ratio"]
self.orientation = data["orientation"]
self.width = data["width"]
self.height = data["height"]
except (IOError, json.JSONDecodeError):
with self.open_image() as image:
self.average_color = calculate_average_color(image)
self.ratio = calculate_ratio(image)
self.orientation = calculate_orientation(image)
width, height = image.size
self.width = width
self.height = height
makedirs(thumbnails_dir, exist_ok=True)
with open(fpath, "w") as f:
json.dump(
{
"average_color": self.average_color,
"ratio": self.ratio,
"orientation": self.orientation,
"width": self.width,
"height": self.height,
},
f,
indent=4,
)
def __lt__(self, other):
return self.path < other.path
@contextmanager
def open_image(self):
with Image.open(self.path) as image:
if image.mode != "RGB":
image = image.convert("RGB")
yield image
@contextmanager
def resized(self, width, height):
thumbnails_dir = path.join(CACHE_DIR, "images", self.hash)
makedirs(thumbnails_dir, exist_ok=True)
fpath = path.join(thumbnails_dir, "{}x{}".format(width, height))
try:
with Image.open(fpath) as image:
yield image
except FileNotFoundError:
with self.open_image() as image:
resized = image.resize((width, height), Image.Resampling.LANCZOS)
resized.save(fpath, "PNG") # TODO: use jpeg for large images
yield resized
def get_grid(self, nb_segments):
with self.resized(nb_segments, nb_segments) as small:
data = small.getdata()
res = []
for i in range(nb_segments):
res.append([])
for j in range(nb_segments):
res[-1].append(data[i * nb_segments + j])
return res