@@ -45,9 +45,9 @@ def save(self, **kwargs):
45
45
46
46
def get_thumbnail_url (self ):
47
47
thumbnail_path = self .get_thumbnail_path (self .thumbnail_size , self .thumbnail_size )
48
- if str ( self . id ) == '4d4d9ae9-3120-4f24-8d57-9d15fa6edfc4' or not default_storage .exists (thumbnail_path ):
48
+ if not default_storage .exists (thumbnail_path ):
49
49
try :
50
- self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * .5 )
50
+ self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size )
51
51
except Exception :
52
52
# thumbnail image could not be created
53
53
return self .fallback_thumbnail_url
@@ -76,7 +76,6 @@ def crop(self, thumbnail_path, width, height):
76
76
aspect_ratio = width / height
77
77
image = Image .open (default_storage .open (self .file_path ))
78
78
image = self .orientate_top (image )
79
- print (f"Wanted size: width={ width } , height={ height } , aspect_ratio={ aspect_ratio } orig={ image .size } " )
80
79
orig_aspect_ratio = image .width / image .height
81
80
crop_x , crop_y , crop_size , gravity = (
82
81
self .meta_data .get ('crop_x' ),
@@ -94,203 +93,65 @@ def crop(self, thumbnail_path, width, height):
94
93
crop_x = 0
95
94
crop_y = (image .height - image .width ) / 2
96
95
crop_resize = crop_size = image .width
97
- # elif width > height:
98
- # if width > crop_size * aspect_ratio:
99
- # # extend the crop size to prevent blurry images
100
- # crop_x = max(crop_x - (width - crop_size) / 2, 0)
101
- # crop_y = max(crop_y - (height - crop_size) / 2, 0)
102
- # crop_size = width
103
96
elif aspect_ratio > 1 :
97
+ # optionally enlarge the crop size to the image height to prevent blurry images
104
98
if height > crop_size :
105
99
crop_resize = min (image .height , height )
106
100
else :
107
101
crop_resize = crop_size
108
102
else :
103
+ # optionally enlarge the crop size to the image width to prevent blurry images
109
104
if width > crop_size :
110
105
crop_resize = min (image .width , width )
111
106
else :
112
107
crop_resize = crop_size
113
108
114
- # calculate the cropped area in image coordinates
109
+ # compute the cropped area in image coordinates
115
110
if aspect_ratio > 1 :
116
111
if aspect_ratio > orig_aspect_ratio :
117
- min_width = max (min (crop_size * aspect_ratio , image .width ), width )
118
- min_height = max (min_width / aspect_ratio , height )
112
+ crop_width = max (min (crop_size * aspect_ratio , image .width ), width )
113
+ crop_height = max (crop_width / aspect_ratio , height )
119
114
else :
120
- min_width = max (min (crop_size , image .height ) * aspect_ratio , width )
121
- min_height = max (min_width / aspect_ratio , height )
115
+ crop_width = max (min (crop_size , image .height ) * aspect_ratio , width )
116
+ crop_height = max (crop_width / aspect_ratio , height )
122
117
else :
123
118
if aspect_ratio < orig_aspect_ratio :
124
- min_height = max (min (crop_size / aspect_ratio , image .height ), height )
125
- min_width = max (min_height * aspect_ratio , width )
119
+ crop_height = max (min (crop_size / aspect_ratio , image .height ), height )
120
+ crop_width = max (crop_height * aspect_ratio , width )
126
121
else :
127
- min_height = max (min (crop_size , image .width ) / aspect_ratio , height )
128
- min_width = max (min_height * aspect_ratio , width )
122
+ crop_height = max (min (crop_size , image .width ) / aspect_ratio , height )
123
+ crop_width = max (crop_height * aspect_ratio , width )
129
124
130
125
# extend the horizontal crop size to prevent blurry images
131
126
if gravity in ('e' , 'ne' , 'se' ):
132
- # if crop_resize > crop_size:
133
- # crop_x # += (width - crop_resize) / 2
134
- # elif aspect_ratio > 1:
135
- # if aspect_ratio > orig_aspect_ratio:
136
- # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1), 0)
137
- # if aspect_ratio < 1:
138
- # if aspect_ratio < orig_aspect_ratio:
139
- # crop_x = max(crop_x + crop_size - image.height * aspect_ratio, 0)
140
- # else:
141
- # crop_x = max(crop_x + crop_resize - min_width, 0)
142
- # minax = max if aspect_ratio > 1 else min
143
- crop_x = max (crop_x + max (crop_resize - min_width , 0 ), 0 )
127
+ crop_x = max (crop_x + max (crop_resize - crop_width , 0 ), 0 )
144
128
elif gravity in ('w' , 'nw' , 'sw' ):
145
- # if crop_resize > crop_size:
146
- # crop_x = max(crop_x + crop_size - width, 0)
147
- # elif aspect_ratio > 1:
148
- crop_x = max (crop_x - max (min (min_width - crop_size , min_width ), 0 ), 0 )
129
+ crop_x = max (crop_x - max (min (crop_width - crop_size , crop_width ), 0 ), 0 )
149
130
else : # centered crop
150
- # if crop_resize > crop_size:
151
- # crop_x = max(crop_x - max(aspect_ratio, 1) * (crop_resize - crop_size) / 2, 0)
152
- # else:
153
- crop_x = max (crop_x - (min_width - crop_size ) / 2 , 0 )
154
-
155
- # if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image.height:
156
- # crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
157
- # elif False and aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image.width:
158
- # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
159
- # elif crop_resize > crop_size:
160
- # crop_x = max(crop_x - offset_x / 2, 0)
131
+ crop_x = max (crop_x - (crop_width - crop_size ) / 2 , 0 )
161
132
162
133
# extend the vertical crop size to prevent blurry images
163
134
if gravity in ('s' , 'se' , 'sw' ):
164
- # if aspect_ratio > 1:
165
- # if aspect_ratio > orig_aspect_ratio:
166
- # crop_y = max(crop_y + crop_size - image.width / aspect_ratio, 0)
167
- # else:
168
- # crop_y = max(crop_y + min_height - crop_resize, 0)
169
-
170
- # if crop_resize > crop_size:
171
- # crop_y = max(crop_y + crop_size - height, 0)
172
- # elif aspect_ratio < 1:
173
- # crop_y = max(crop_y + crop_resize - min_height, 0)
174
- crop_y = max (crop_y + max (crop_resize - min_height , 0 ), 0 )
135
+ crop_y = max (crop_y + max (crop_resize - crop_height , 0 ), 0 )
175
136
elif gravity in ('n' , 'ne' , 'nw' ):
176
- # if crop_resize >= crop_size:
177
- # crop_y # += (height - crop_resize) / 2
178
- # elif aspect_ratio < 1:
179
- # if aspect_ratio < orig_aspect_ratio:
180
- # crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio), 0)
181
- # else:
182
- # crop_y
183
- # else:
184
- # crop_y = max(crop_y + (crop_size - image.width / aspect_ratio), 0)
185
- crop_y = max (crop_y - max (min (min_height - crop_size , min_height ), 0 ), 0 )
137
+ crop_y = max (crop_y - max (min (crop_height - crop_size , crop_height ), 0 ), 0 )
186
138
else : # centered crop
187
- # if crop_resize > crop_size:
188
- # crop_y = max(crop_y - (crop_resize - crop_size) / min(aspect_ratio, 1) / 2, 0)
189
- # else:
190
- crop_y = max (crop_y - (min_height - crop_size ) / 2 , 0 )
191
-
192
- # if aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio > image.height:
193
- # crop_x = max(crop_x + (crop_size - image.height * aspect_ratio) / 2, 0)
194
- # elif aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio < image.width:
195
- # crop_x = max(crop_x - crop_resize * (aspect_ratio - 1) / 2, 0)
196
- # if aspect_ratio > orig_aspect_ratio and crop_resize * aspect_ratio > image.width:
197
- # crop_y = max(crop_y + crop_resize * (1 - 1 / aspect_ratio) / 2, 0)
198
- # elif False and aspect_ratio < orig_aspect_ratio and crop_resize / aspect_ratio < image.height:
199
- # crop_y = max(crop_y + (crop_size - image.width / aspect_ratio) / 2, 0)
200
- # elif crop_resize > crop_size:
201
- # crop_y = max(crop_y - offset_y / 2, 0)
139
+ crop_y = max (crop_y - (crop_height - crop_size ) / 2 , 0 )
202
140
crop_size = crop_resize
203
141
204
- print (f"Crop parameters: crop_x={ crop_x } , crop_y={ crop_y } , crop_size={ crop_size } , gravity={ gravity } " )
205
-
206
- if False :
207
- min_x = max (crop_x + (crop_size - min_width ) / 2 , 0 )
208
- else :
209
- min_x = crop_x
210
-
211
- if min_x + min_width > image .width :
212
- min_x = max (image .width - min_width , 0 )
142
+ min_x = crop_x
143
+ if min_x + crop_width > image .width :
144
+ min_x = max (image .width - crop_width , 0 )
213
145
max_x = image .width
214
146
else :
215
- max_x = min_x + min_width
216
- if False :
217
- min_y = max (crop_y + (crop_size - min_height ) / 2 , 0 )
218
- else :
219
- min_y = crop_y
220
- if min_y + min_height > image .height :
221
- min_y = max (image .height - min_height , 0 )
147
+ max_x = min_x + crop_width
148
+ min_y = crop_y
149
+ if min_y + crop_height > image .height :
150
+ min_y = max (image .height - crop_height , 0 )
222
151
max_y = image .height
223
152
else :
224
- max_y = min_y + min_height
225
-
226
- if False :
227
- # horizontal thumbnailing
228
- if orig_height / crop_size < aspect_ratio :
229
- # we can't fill the thumbnailed image with the cropped part
230
- min_width = orig_width * crop_size / orig_height
231
- min_x = (orig_width - min_width ) / 2
232
- elif aspect_ratio < 1 :
233
- min_width = max (crop_size * aspect_ratio * 3 / 2 , width )
234
- min_x = crop_x + (crop_size - min_width ) / 2
235
- else :
236
- min_width = max (crop_size , width )
237
- if crop_size < min_width :
238
- min_x = max (crop_x - (min_width - crop_size ) / 2 , 0 )
239
- else :
240
- min_x = crop_x
241
- if crop_size < min_width :
242
- # thumbnail would be smaller than the crop size, avoid blurry image
243
- if gravity in ('e' , 'ne' , 'se' ):
244
- max_x = min (crop_x + min_width , image .width )
245
- min_x = max (max_x - min_width , 0 )
246
- elif gravity in ('w' , 'nw' , 'sw' ):
247
- min_x = max (crop_x - min_width + crop_size , 0 )
248
- if min_x + min_width > image .width :
249
- min_x = max (image .width - min_width , 0 )
250
- max_x = image .width
251
- else :
252
- max_x = min_x + min_width
253
-
254
- # vertical thumbnailing
255
-
256
- # if False and orig_width / crop_size < aspect_ratio:
257
- # # we can't fill the thumbnailed image with the cropped part
258
- # min_height = orig_height
259
- # min_y = 0
260
- if aspect_ratio > 1 :
261
- min_height = max (crop_size * aspect_ratio * 3 / 2 , height )
262
- min_y = crop_y + (crop_size - min_height ) / 2
263
- else :
264
- min_height = max (crop_size / aspect_ratio , height )
265
- if crop_size < min_height :
266
- min_y = max (crop_y - (min_height - crop_size ) / 2 , 0 )
267
- else :
268
- min_y = crop_y
269
-
270
- # if orig_width < orig_height:
271
- # min_height = max(crop_size / aspect_ratio, height)
272
- # else:
273
- # min_height = max(min_width / aspect_ratio, height)
274
- # if aspect_ratio > 1:
275
- # min_y = crop_y + (crop_size - min_height) / 2
276
- # elif crop_size < min_height:
277
- # min_y = crop_y - (min_height - crop_size) / 2
278
- # else:
279
- # min_y = crop_y
280
-
281
- if crop_size < min_height :
282
- if gravity in ('s' , 'se' , 'sw' ):
283
- max_y = min (crop_y + min_height , image .height )
284
- min_y = max (max_y - min_height , 0 )
285
- elif gravity in ('n' , 'ne' , 'nw' ):
286
- min_y = max (crop_y - min_height + crop_size , 0 )
287
- if min_y + min_height > image .height :
288
- min_y = max (image .height - min_height , 0 )
289
- max_y = image .height
290
- else :
291
- max_y = min_y + min_height
153
+ max_y = min_y + crop_height
292
154
293
- print (f"Crop area: ({ min_x } , { min_y } ) to ({ max_x } , { max_y } ) = { max_x - min_x } x { max_y - min_y } " )
294
155
image = image .crop ((min_x , min_y , max_x , max_y ))
295
156
image .thumbnail ((width , height ))
296
157
(default_storage .base_location / thumbnail_path .parent ).mkdir (parents = True , exist_ok = True )
0 commit comments