@@ -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 ) == '4d4d9ae9-3120-4f24-8d57-9d15fa6edfc4' or not default_storage .exists (thumbnail_path ):
48+ if not default_storage .exists (thumbnail_path ):
4949 try :
50- self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * .5 )
50+ self .crop (thumbnail_path , self .thumbnail_size , self .thumbnail_size * 1.8 )
5151 except Exception :
5252 # thumbnail image could not be created
5353 return self .fallback_thumbnail_url
@@ -94,12 +94,6 @@ def crop(self, thumbnail_path, width, height):
9494 crop_x = 0
9595 crop_y = (image .height - image .width ) / 2
9696 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
10397 elif aspect_ratio > 1 :
10498 if height > crop_size :
10599 crop_resize = min (image .height , height )
@@ -114,181 +108,50 @@ def crop(self, thumbnail_path, width, height):
114108 # calculate the cropped area in image coordinates
115109 if aspect_ratio > 1 :
116110 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 )
111+ crop_width = max (min (crop_size * aspect_ratio , image .width ), width )
112+ crop_height = max (crop_width / aspect_ratio , height )
119113 else :
120- min_width = max (min (crop_size , image .height ) * aspect_ratio , width )
121- min_height = max (min_width / aspect_ratio , height )
114+ crop_width = max (min (crop_size , image .height ) * aspect_ratio , width )
115+ crop_height = max (crop_width / aspect_ratio , height )
122116 else :
123117 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 )
118+ crop_height = max (min (crop_size / aspect_ratio , image .height ), height )
119+ crop_width = max (crop_height * aspect_ratio , width )
126120 else :
127- min_height = max (min (crop_size , image .width ) / aspect_ratio , height )
128- min_width = max (min_height * aspect_ratio , width )
121+ crop_height = max (min (crop_size , image .width ) / aspect_ratio , height )
122+ crop_width = max (crop_height * aspect_ratio , width )
129123
130124 # extend the horizontal crop size to prevent blurry images
131125 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 )
126+ crop_x = max (crop_x + max (crop_resize - crop_width , 0 ), 0 )
144127 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 )
128+ crop_x = max (crop_x - max (min (crop_width - crop_size , crop_width ), 0 ), 0 )
149129 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)
130+ crop_x = max (crop_x - (crop_width - crop_size ) / 2 , 0 )
161131
162132 # extend the vertical crop size to prevent blurry images
163133 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 )
134+ crop_y = max (crop_y + max (crop_resize - crop_height , 0 ), 0 )
175135 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 )
136+ crop_y = max (crop_y - max (min (crop_height - crop_size , crop_height ), 0 ), 0 )
186137 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)
138+ crop_y = max (crop_y - (crop_height - crop_size ) / 2 , 0 )
202139 crop_size = crop_resize
203140
204141 print (f"Crop parameters: crop_x={ crop_x } , crop_y={ crop_y } , crop_size={ crop_size } , gravity={ gravity } " )
205142
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 )
143+ min_x = crop_x
144+ if min_x + crop_width > image .width :
145+ min_x = max (image .width - crop_width , 0 )
213146 max_x = image .width
214147 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 )
148+ max_x = min_x + crop_width
149+ min_y = crop_y
150+ if min_y + crop_height > image .height :
151+ min_y = max (image .height - crop_height , 0 )
222152 max_y = image .height
223153 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
154+ max_y = min_y + crop_height
292155
293156 print (f"Crop area: ({ min_x } , { min_y } ) to ({ max_x } , { max_y } ) = { max_x - min_x } x { max_y - min_y } " )
294157 image = image .crop ((min_x , min_y , max_x , max_y ))
0 commit comments