Skip to content

Commit 914753e

Browse files
committed
puh, gravity cropping seems to work now
1 parent 8a4bf96 commit 914753e

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

finder/contrib/image/pil/models.py

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ def save(self, **kwargs):
4545

4646
def get_thumbnail_url(self):
4747
thumbnail_path = self.get_thumbnail_path(self.thumbnail_size, self.thumbnail_size)
48-
if str(self.id) == 'XXX-561d6907-5fd7-40b9-a6f2-5db45a67eda6' or not default_storage.exists(thumbnail_path):
48+
if str(self.id) == '4d4d9ae9-3120-4f24-8d57-9d15fa6edfc4' or not default_storage.exists(thumbnail_path):
4949
try:
50-
self.crop(thumbnail_path, self.thumbnail_size, self.thumbnail_size * 1.667)
50+
self.crop(thumbnail_path, self.thumbnail_size, self.thumbnail_size * 0.8)
5151
except Exception:
5252
# thumbnail image could not be created
5353
return self.fallback_thumbnail_url
@@ -84,48 +84,80 @@ def crop(self, thumbnail_path, width, height):
8484
self.meta_data.get('crop_size'),
8585
self.meta_data.get('gravity'),
8686
)
87-
next_crop_size = None
8887
if crop_x is None or crop_y is None or crop_size is None:
8988
# crop in the center of the image
9089
if image.width > image.height:
9190
crop_x = (image.width - image.height) / 2
9291
crop_y = 0
93-
crop_size = image.height
92+
crop_resize = crop_size = image.height
9493
else:
9594
crop_x = 0
9695
crop_y = (image.height - image.width) / 2
97-
crop_size = image.width
96+
crop_resize = crop_size = image.width
9897
# elif width > height:
9998
# if width > crop_size * aspect_ratio:
10099
# # extend the crop size to prevent blurry images
101100
# crop_x = max(crop_x - (width - crop_size) / 2, 0)
102101
# crop_y = max(crop_y - (height - crop_size) / 2, 0)
103102
# crop_size = width
104103
elif aspect_ratio > 1:
105-
if width > crop_size:
106-
offset = width - crop_size
107-
next_crop_size = width
108-
else:
109104
if height > crop_size:
110-
offset = height - crop_size
111-
next_crop_size = height
112-
if next_crop_size is not None:
113-
# extend the crop size to prevent blurry images
114-
if gravity in ('e', 'ne', 'se'):
115-
crop_x = max(crop_x - (width - crop_size) / 2, 0)
116-
# crop_x = max(crop_x - (width - next_crop_size) / 2, 0)
117-
elif gravity in ('w', 'nw', 'sw'):
105+
crop_resize = min(image.height, width)
106+
else:
107+
crop_resize = crop_size
108+
else:
109+
if width > crop_size:
110+
crop_resize = min(image.width, height)
111+
else:
112+
crop_resize = crop_size
113+
114+
# extend the horizontal crop size to prevent blurry images
115+
if gravity in ('e', 'ne', 'se'):
116+
if crop_resize > crop_size:
117+
crop_x += (width - crop_resize) / 2
118+
elif aspect_ratio > 1:
119+
crop_x = max(crop_x - crop_resize * (aspect_ratio - 1), 0)
120+
else:
121+
crop_x = max(crop_x + crop_size - image.height * aspect_ratio, 0)
122+
elif gravity in ('w', 'nw', 'sw'):
123+
if crop_resize > crop_size:
118124
crop_x = max(crop_x - width + crop_size, 0)
125+
elif aspect_ratio > 1:
126+
crop_x = max(crop_x - crop_resize * (aspect_ratio - 1), 0)
127+
else: # centered crop
128+
if crop_resize > crop_size:
129+
# crop_x = max(crop_x - (width - crop_resize) / 2, 0)
130+
# crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
131+
# crop_x = max(crop_x + (crop_resize - image.height * aspect_ratio) / 2, 0)
132+
crop_x = max(crop_x - (crop_resize - crop_size) / 2, 0)
133+
elif aspect_ratio > 1:
134+
crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
119135
else:
120-
crop_x = max(crop_x - (next_crop_size - crop_size) / 2, 0)
121-
if gravity in ('n', 'ne', 'nw'):
136+
crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
137+
138+
# extend the vertical crop size to prevent blurry images
139+
if gravity in ('n', 'ne', 'nw'):
140+
if crop_resize > crop_size:
122141
crop_y = max(crop_y - height + crop_size, 0)
123-
elif gravity in ('s', 'se', 'sw'):
142+
elif aspect_ratio < 1:
143+
crop_y = max(crop_y + crop_size * (1 - 1 / aspect_ratio), 0)
144+
elif gravity in ('s', 'se', 'sw'):
145+
if crop_resize > crop_size:
146+
crop_y += (height - crop_resize) / 2
147+
elif aspect_ratio < 1:
148+
crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio), 0)
149+
else:
150+
crop_y = max(crop_y + (crop_size - image.width / aspect_ratio), 0)
151+
else:
152+
if crop_resize > crop_size:
153+
# if aspect_ratio < 1:
124154
crop_y = max(crop_y - (height - crop_size) / 2, 0)
125-
# crop_y = max(crop_y - (height - next_crop_size) / 2, 0)
155+
elif aspect_ratio < 1:
156+
crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio) / 2, 0)
126157
else:
127-
crop_y = max(crop_y - (next_crop_size - crop_size) / 2, 0)
128-
crop_size = next_crop_size
158+
crop_y = max(crop_y + (crop_size - image.width / aspect_ratio) / 2, 0)
159+
160+
crop_size = crop_resize
129161

130162
print(f"Crop parameters: crop_x={crop_x}, crop_y={crop_y}, crop_size={crop_size}, gravity={gravity}")
131163

@@ -147,13 +179,19 @@ def crop(self, thumbnail_path, width, height):
147179
# min_height = image.width / aspect_ratio
148180
min_height = min(crop_size, image.width / aspect_ratio)
149181
min_width = min_height * aspect_ratio
150-
min_x = max(crop_x + (crop_size - min_width) / 2, 0)
182+
if False:
183+
min_x = max(crop_x + (crop_size - min_width) / 2, 0)
184+
else:
185+
min_x = crop_x
151186
if min_x + min_width > image.width:
152187
min_x = max(image.width - min_width, 0)
153188
max_x = image.width
154189
else:
155190
max_x = min_x + min_width
156-
min_y = max(crop_y + (crop_size - min_height) / 2, 0)
191+
if False:
192+
min_y = max(crop_y + (crop_size - min_height) / 2, 0)
193+
else:
194+
min_y = crop_y
157195
if min_y + min_height > image.height:
158196
min_y = max(image.height - min_height, 0)
159197
max_y = image.height

0 commit comments

Comments
 (0)